2014-06-30 65 views
2

Iam新的asp.net和我一直在尝试上述问题一段时间没有任何成功。这里是我的代码在roweditevent如何在点击编辑按钮时在gridview中获得文本框的值?

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); 
    } 
+0

我猜你txtboxname没有被发现。你在哪条线上得到例外? – Matthijs

+0

在第四行 – krishb591993

+0

但我已经在行更新事件中使用了相同的代码,它工作正常 – krishb591993

回答

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"); 
    } 
} 
+0

所以我们只是无法获得GridView1_RowEditing(对象发件人,GridViewEditEventArgs e)事件 – krishb591993

+0

内部的值可能会有其他方法,但这是我做的,以完成我的工作。 –

+0

它的工作人员.thanx .. – krishb591993

0

在行数据绑定事件跟踪行状态,并进行必要的调用来获取所需的控制的GridViewRow对象上调用FindControl()的参考。从here

+0

它不工作的人。 ANYWAY,thanx帮助 – krishb591993

+0

当您单击具有文本框列的模板字段列上的编辑按钮时,此代码将运行,并且网格正在编辑模式下进行数据绑定。换句话说,情景是,你想用默认值填充文本框,你会这样做。在我回答问题之前,这与你最近表达的目的完全不同。 – deostroll

+0

好的,但我的问题是要获取GridView文本框中的值,我现在得到它 – krishb591993

0

采取

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"; 
    } 
} 

代码本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 
} 
+0

但是,我使用eventargs的索引,你可以在我的代码中看到。 – krishb591993

+0

您正在使用e.RowIndex NOT e.NewEditIndex – StackTrace

+0

问题出在编辑代码中,并且在该部分中,Iam使用e.neweditindex – krishb591993

相关问题