【问题标题】:How to get textbox value in gridview on clicking edit button?单击编辑按钮时如何在gridview中获取文本框值?
【发布时间】:2014-06-30 06:18:27
【问题描述】:

我是 asp.net 的新手,我一直在尝试上述问题一段时间,但没有任何成功。这是我在 roweditevent 中的代码

 GridView1.EditIndex = e.NewEditIndex;
 int newindex = e.NewEditIndex;
 TextBox NAME = GridView1.Rows[newindex].FindControl("txtboxname") as TextBox;
 string gridupdat = NAME.Text;

但在调试时,我总是在那里得到空引用。

这是我的行更新代码,它工作正常

Label ID = GridView1.Rows[e.RowIndex].FindControl("ID") as Label;
    TextBox NAME = GridView1.Rows[e.RowIndex].FindControl("txtboxname") as TextBox;
    DropDownList STATUS = GridView1.Rows[e.RowIndex].FindControl("dropdownstatus") as DropDownList;
    string string1 = NAME.Text;
    if (fetchmail(string1, labelgrid999) == true)
    {
        string updatquery = string.Format("UPDATE Compliance_Tracker.dbo.verificationMaster SET NAME='{0}',STATUS='{1}' WHERE ID = {2}", NAME.Text, STATUS.Text, Convert.ToInt32(ID.Text));
        string dupquery = "select COUNT(*) from Compliance_Tracker.dbo.verificationMaster where Compliance_Tracker.dbo.verificationMaster.NAME = '" + NAME.Text + "';";
        if (obj4.isDuplicate(dupquery) == false )
        {
            GridView1.EditIndex = -1;
            string populatequery = updatquery + ";select NAME,(case when STATUS='1' then 'Active' when STATUS='0'then 'Inactive' ELSE 'UNKNOWN' END)as STATUS,ID from Compliance_Tracker.dbo.verificationMaster;";
            obj4.BindGridData(populatequery, GridView1);
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + "Username already exists" + "');", true);
        }
    }
    else
    {
        string myStringVariable = "Please enter valid username";
        ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + myStringVariable + "');", true);
    }

【问题讨论】:

  • 我猜你的 txtboxname 没有被找到。您在哪一行得到异常?
  • 但我在行更新事件中使用了相同的代码,它工作正常
  • 所以没有找到Name。你确定 txtboxname 实际上是正确的吗?
  • 是的,我也应该给行更新代码
  • 拜托,如果有人能找到问题所在,请发布...

标签: c# asp.net gridview


【解决方案1】:

当我尝试在编辑按钮单击时从文本框中获取数据时,我会这样做:

<asp:TemplateField HeaderText="Edit">
   <ItemTemplate>
      <asp:Button ID="BtnEdit" CausesValidation="false" runat="server" Text="Update" CommandName="updateData" CommandArgument='<%# Eval("ID") %>' />
   </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Product Price">
   <ItemTemplate>
       <asp:TextBox ID="TxtPriceEdit" runat="server" Text='<%# Eval("Product_Price") %>' Width="120px"></asp:TextBox>
   </ItemTemplate>
</asp:TemplateField>

代码背后:

protected void GrdDataEdit_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "updateData")
    {
        // This will give you the ID of the record you are passing in the CommandArgument='<%# Eval("ID") %>'  
        int i = Convert.ToInt32(e.CommandArgument); 

        GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
        TextBox price = (TextBox)row.FindControl("TxtPriceEdit");
    }
}

【讨论】:

  • 所以我们无法获取 GridView1_RowEditing(object sender, GridViewEditEventArgs e) 事件中的值
  • 可能没有其他方法,但这就是我所做的,以完成我的工作。
  • 没关系,一切都是为了帮助,信誉不算数。很高兴为您提供帮助。
【解决方案2】:

行数据绑定事件跟踪行状态并进行必要的调用以获取所需控件调用FindControl()GridViewRow 对象的引用。

protected void gvMaint_RowDataBound(Object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowState == DataControlRowState.Edit)
    {
        TextBox txtFreqMiles = (TextBox)e.Row.FindControl("txtFreqMiles");

        // At this point, you can change the value as normal
        txtFreqMiles.Text = "some new text";
    }
}

代码取自here

【讨论】:

  • 当您单击具有文本框列的模板字段列上的编辑按钮时,此代码将运行,并且网格在编辑模式下被数据绑定。换句话说,场景是,你想用默认值填充文本框,你会做这样的事情。在我回答问题之前,这与您最近陈述的目的完全不同。
  • 好的,但我的问题是在我现在得到的 gridview 文本框中获取值
  • @user3789152 好的。没问题。只需确保下次发布问题时,提供准确的详细信息以帮助重现问题或帮助其他人更好地快速理解。欢迎来到 SO,祝您编程愉快。 :)
【解决方案3】:

GridViewEventArgs 具有正在编辑的行的索引。您似乎没有使用事件参数中的索引。试试这个:

protected void edit(object sender, GridViewEditEventArgs e)         
{

    string gridupdat = GridView1.Rows[e.NewEditIndex].Cells[0].Text;
    // where Cells[0] = the index of your "txtboxname" column
}

【讨论】:

  • 但是,正如您在我的代码中看到的那样,我正在使用来自 eventargs 的索引。
  • 您使用的是 e.RowIndex 而不是 e.NewEditIndex
  • 问题出在编辑代码和那部分,我正在使用 e.neweditindex
  • @SQL.NETWarrior:第一行。
猜你喜欢
  • 2018-03-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-22
  • 2013-04-16
  • 1970-01-01
  • 1970-01-01
  • 2012-05-14
  • 2011-12-17
相关资源
最近更新 更多