【问题标题】:Accessing Textbox from GridView data cell (ASP.NET)从 GridView 数据单元 (ASP.NET) 访问文本框
【发布时间】:2011-04-26 16:58:36
【问题描述】:
      protected void GridView2_OnCommand(Object sender, GridViewCommandEventArgs e)

        {
            if (e.CommandName == "Reply")
            {
                con = new System.Data.SqlClient.SqlConnection();
                con.ConnectionString = "Data Source=myconnectionstring; Integrated Security = true; Connect Timeout = 30; User Instance = True";
                con.Open();

                string div = "','";
                GridViewRow selectedRow = GridView2.Rows[Convert.ToInt32(e.CommandArgument)];
                SqlCommand cmd = new SqlCommand("INSERT INTO SellerResponse VALUES ('" +
                    //THIS LINE IS THE ISSUE
                Request.QueryString["ID"] + div + selectedRow.Cells[2].Text + div + DateTime.Now.ToString() + div + selectedRow.Cells[3].Text + ((System.Web.UI.WebControls.TextBox)(FindControl(selectedRow.Cells[1].UniqueID))).Text /*this is the cell that contains the textbox*/+ "');", con);
            }
        }

有什么想法可以实现吗?

【问题讨论】:

  • 一行代码中塞满了太多东西,你永远无法调试它。即使你让它工作,它也将无法维护。将每个逻辑步骤分解为单独的代码行。

标签: asp.net gridview


【解决方案1】:
 protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
        {

            try
            {
                int index = Convert.ToInt32(e.CommandArgument);

                if (e.CommandName == "Reply")
                {

                   on = new System.Data.SqlClient.SqlConnection();
                   con.ConnectionString = "Data Source=myconnectionstring; Integrated Security = true; Connect Timeout = 30; User Instance = True";
                   con.Open();

                   string div = "','";
                   GridViewRow selectedRow = GridView2.Rows[Convert.ToInt32(e.CommandArgument)];
                   SqlCommand cmd = new SqlCommand("INSERT INTO SellerResponse VALUES ('" +
                   //THIS LINE IS THE ISSUE
                   Request.QueryString["ID"] + div + GridView2.Rows[index].Cells[2].Text + div + DateTime.Now.ToString() + div + GridView2.Rows[index].Cells[3].Text + ((System.Web.UI.WebControls.TextBox)(GridView2.Rows[index]Cells[1].FindControl("the name of the text box")).Text /*this is the cell that contains the textbox*/+ "');", con);

                }
            }


            catch (Exception ee)
            {
                string message = ee.Message;
            }
        }

注意:1- in aspx :CommandArgument='<%#((GridViewRow)Container).RowIndex%>'

   2- use RowCommand event for the grid view.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多