1

我在 ASP.Net webapp 中有一个工作的 GridView,它跟踪我请求的 DNS 记录。它的定义如下:

<asp:GridView ID="gvExistingEntries" runat="server" AutoGenerateColumns="False" 
    DataKeyNames="DNSId" DataSourceID="dsDNS" Width="100%" 
    BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" 
    CellPadding="4" ForeColor="Black" GridLines="Vertical" AllowSorting="True" ShowFooter="true">
    <AlternatingRowStyle BackColor="White" />
    <Columns>
        <asp:BoundField DataField="DNSId" Visible="false" ReadOnly="true" />
        <asp:BoundField DataField="DNSName" HeaderText="DNS Name" SortExpression="DNSName" />
        <asp:BoundField DataField="IPAddress" HeaderText="IP Address" ReadOnly="True" SortExpression="IPAddress" />
        <asp:BoundField DataField="RecordType" HeaderText="Record Type" SortExpression="RecordType" />
        <asp:BoundField DataField="RequestTicket" HeaderText="Request Ticket" SortExpression="RequestTicket" />
        <asp:BoundField DataField="LastUpdateBy" HeaderText="Updated By" ReadOnly="True" SortExpression="LastUpdateBy" />

        <asp:TemplateField ShowHeader="false" ItemStyle-HorizontalAlign="Center">
            <ItemTemplate>
                <asp:LinkButton ID="updateButton" runat="server" CommandName="Edit" Text="Update" />
                <asp:LinkButton ID="deleteButton" runat="server" CommandName="Delete" Text="Delete" onClientClick="return confirm('Are you sure you want to delete this entry?');" />
            </ItemTemplate>

            <EditItemTemplate>
                <asp:LinkButton ID="saveUpdate" runat="server" CommandName="Update" Text="Save" onClientClick="return confirm('Are you sure you want to save these changes?');" />
                <asp:LinkButton ID="cancelUpdate" runat="server" CommandName="Cancel" Text="Cancel" />
            </EditItemTemplate>
        </asp:TemplateField>
    </Columns>
    <FooterStyle BackColor="#CCCC99" />
    <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
    <RowStyle BackColor="#F7F7DE" />
    <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#FBFBF2" />
    <SortedAscendingHeaderStyle BackColor="#848384" />
    <SortedDescendingCellStyle BackColor="#EAEAD3" />
    <SortedDescendingHeaderStyle BackColor="#575357" />
</asp:GridView>

数据源定义如下:

<asp:SqlDataSource ID="dsDNS" runat="server" 
    ConnectionString="<%$ ConnectionStrings:DNSMonConnectionString %>" 
    SelectCommand="SELECT [DNSId], [DNSName], [RecordType], [StartValidityDate], [EndValidityDate], [IPAddress], [LastUpdate], [RequestTicket], (select [FirstName] + ' ' + [LastName] + ' (' + [Login] + ')' FROM [User] where [User].[UserID] = [DomainName].[LastUpdateBy]) AS [LastUpdateBy] FROM [DomainName] ORDER BY [DNSName]"

    UpdateCommandType="StoredProcedure"
    UpdateCommand="sp_update_DNSEntry" OnUpdating
    DeleteCommandType="StoredProcedure"
    DeleteCommand="sp_drop_DNSEntry">
    <DeleteParameters>
        <asp:Parameter Name="DNSId" Type="Int32" />
    </DeleteParameters>
    <UpdateParameters>
        <asp:Parameter Name="DNSId" />
        <asp:Parameter Name="DNSName" />
        <asp:Parameter Name="RecordType" />
        <asp:Parameter Name="IPAddress" />
        <asp:Parameter Name="RequestTicket" />
    </UpdateParameters>
</asp:SqlDataSource>

每当用户使用更新功能时,它都会起作用并将其保存到数据库中,但我希望代码在保存之前解析 IP 地址。我已经准备好为此使用的方法,但我不知道如何将其合并到代码中。

如果可能的话,我宁愿避免将代码放在数据库中。

4

2 回答 2

0

OnUpdating您可以使用事件 在更新前更新 IP 地址。

<asp:SqlDataSource OnUpdating="OnSqlUpdating"
    ... etc />

在处理程序代码中,从 SqlDataSourceCommandEventArgs 获取Command属性,并更新相应的参数。

private void OnSqlUpdating(Object source, SqlDataSourceCommandEventArgs e) 
{
    DbCommand command = e.Command;
    command.Parameters["IPAddress"] = Request.UserHostAddress;
}
于 2012-11-07T05:48:22.533 回答
0

对于任何有兴趣的人,实现这一点的代码是:

<asp:SqlDataSource ID="dsDNS" runat="server" OnUpdating="OnSqlUpdating" ... />

protected void OnSqlUpdating(Object source, SqlDataSourceCommandEventArgs e)
{
    string sIPAddresses = "";

    // Custom code here to look up IP addresses.

    DbCommand dbCmd = e.Command;
    dbCmd.Parameters["@IPAddress"].Value = sIPAddresses;
}
于 2012-11-08T04:00:50.643 回答