简体   繁体   中英

Request.QueryString in Gridview

I have this ImageButton and Label in my GridView

Grid Definition is as follows

<asp:TemplateField HeaderText="Send kwm">
    <ItemTemplate>
        <center>
            <asp:ImageButton ID="Sendkwm" runat="server" ImageUrl="/Images/check.gif"
                OnClick="Sendkwm" />
        </center>
    </ItemTemplate>
</asp:TemplateField>

<asp:TemplateField>
    <ItemTemplate>
        <center>
            <asp:Label ID="kwm" runat="server" Text='<%# Eval("kwm").ToString() %>'></asp:Label>
        </center>
    </ItemTemplate>
</asp:TemplateField>

My problem is I need to use the querystring value of kwm to update which column of data I'm going to display.

I searched on the web but it seems that it is necessary to use the GridView the SqlDataSource, is there any alternative to do this alternative method ?

My code-behind below.

Any help would be appreciated, thank you in advance.

protected void Sendkwm(object sender, EventArgs e)
{
    using (OdbcConnection conn =
        new OdbcConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString))
    {
        sql = " UPDATE doTable " +
               " SET myDate = CURRENT_TIMESTAMP () " +
               " WHERE " +
               "    kwm =  //here the querystring value of kwm// ; ";

        using (OdbcCommand command =
            new OdbcCommand(sql,conn))
        {
            try
            {
                command.Connection.Open();
                command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                command.Connection.Close();
            }
        }
    }
}

I would simply use the codebehind, especially the GridView 's RowDataBound -event . That makes your code also more robust since you get compile time safety and it's also more readable/maintainable:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        ImageButton sendKwm =  (ImageButton) e.Row.FindControl("Sendkwm ");
        Label lblKwm = (Label) e.Row.FindControl("kwm");
        lblKwm.Text = Request.QueryString["kwm"]; 
    }
}

Now you get the value in the ImageButton 's click-event handler in the following way:

protected void Sendkwm(object sender, EventArgs e)
{
    ImageButton sendKwm = (ImageButton)sender;
    GridViewRow row = (GridViewRow) sendKwm.NamingContainer;
    Label lblKwm = (Label)row.FindControl("kwm");

    using (OdbcConnection conn = new OdbcConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString))
    {
        string sql = @"UPDATE doTable 
                       SET myDate = CURRENT_TIMESTAMP()
                       WHERE kwm = @kwm;";

        using (OdbcCommand command = new OdbcCommand(sql, conn))
        {
            try
            {
                command.Parameters.AddWithValue("@kwm", lblKwm.Text);
                command.Connection.Open();
                command.ExecuteNonQuery();
            } catch (Exception ex)
            {
                throw ex;
            } finally
            {
                command.Connection.Close();
            }
        }
    }
}

Also note that i've used sql-parameters to prevent sql injection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM