Passing the Clients' IP address to text box under InsertItemTemplate in FormView for submitting to database

StackOverflow https://stackoverflow.com/questions/18331687

  •  25-06-2022
  •  | 
  •  

문제

I am trying to save clients IP address who are submitting the data on my website. I have downloaded an example and implemented. That scrip displays IP address on the webpage. I can submit this to sql table through code behind. Now I want to use same script in FormView and I am unable to bind this with the text box as it is showing an error "The name IPaddress does not exist in the current context". Please correct the following script to do so. Thanks...

<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
    string IPAdd = string.Empty;
    IPAdd = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
    if (string.IsNullOrEmpty(IPAdd))
        IPAdd = Request.ServerVariables["REMOTE_ADDR"];
    IPaddress.Text = IPAdd;
}
</script>

</head>

<body>

<form id="form1" runat="server">

<div>
 <asp:FormView ID="FormView1" runat="server" DataKeyNames="ID" 
    DataSourceID="SqlDataSource1" EnableModelValidation="True" DefaultMode="Insert" >
            <InsertItemTemplate>
        Your IP address:
        <asp:TextBox ID="IPaddress" runat="server" Font-Bold="true" Text='<%# Bind("IPadr") %>' />
        <br />
        Submitted by:
        <asp:TextBox ID="Submittedby" runat="server" Font-Bold="true" Text='<%# Bind("sub_by") %>' />
        <br />
        <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" 
            CommandName="Insert" Text="Insert" />
        &nbsp;<asp:LinkButton ID="InsertCancelButton" runat="server" 
            CausesValidation="False" CommandName="Cancel" Text="Cancel" />
            </InsertItemTemplate>
    </asp:FormView>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:IPaddressConnectionString %>" 
    DeleteCommand="DELETE FROM [mytable] WHERE [ID] = @ID" 
    InsertCommand="INSERT INTO [mytable] ([IPadr], [sub_by]) VALUES (@IPadr, @sub_by)" 
    SelectCommand="SELECT * FROM [mytable]"> 
    <DeleteParameters>
        <asp:Parameter Name="ID" Type="Int32" />
    </DeleteParameters>
    <InsertParameters>
        <asp:Parameter Name="IPadr" Type="String" />
        <asp:Parameter Name="sub_by" Type="String" />
    </InsertParameters>
    <UpdateParameters>
        <asp:Parameter Name="IPadr" Type="String" />
        <asp:Parameter Name="sub_by" Type="String" />
        <asp:Parameter Name="ID" Type="Int32" />
    </UpdateParameters>
</asp:SqlDataSource>
도움이 되었습니까?

해결책

I have achieved by using the following script with OnDataBound event:

string IPaddress;


protected void FormView1_OnDataBound(object sender, System.EventArgs e)
{
    if (FormView1.CurrentMode == FormViewMode.Insert)
    {

        TextBox IPAdd = (TextBox)FormView1.FindControl("IPaddress");
        if (IPAdd != null)
        {

            IPAdd.Text = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

            if (string.IsNullOrEmpty(IPAdd.Text))

                IPAdd.Text = Request.ServerVariables["REMOTE_ADDR"];

        }

    }

}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top