我有以下代码:
protected void GridView4_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "UpdateGCommand")
{
if (IsPostBack)
{
DataSet EditT = new DataSet();
DataSet ValidT = new DataSet();
DataRow row;
if (Session["Edit"] != null)
{
EditT = (DataSet)Session["Edit"];
}
if (Session["Valid"] != null)
{
ValidT = (DataSet)Session["Valid"];
}
DataTable dtEdit = EditT.Tables[0];
DataTable dtValid = ValidT.Tables[0];
GridViewRow gvr = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
int RowIndex = gvr.RowIndex;
row = dtEdit.Rows[RowIndex];
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
// Must be able to update the EditT or dtEdit with value from Gridview4 textboxes here
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
dtValid.Rows.Add(row.ItemArray);
dtEdit.Rows[RowIndex].Delete();
GridView4.DataSource = EditT;
GridView5.DataSource = ValidT;
GridView4.DataBind();
GridView5.DataBind();
}
}
}
和以下asp:
<asp:GridView ID="GridView4" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None" OnRowCommand="GridView4_RowCommand"
OnSelectedIndexChanged="GridView4_SelectedIndexChanged" AutoGenerateColumns="False">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton OnClick="UpdateRow_Click"
ID="LinkButton1"
runat="server"
CausesValidation="false"
CommandName="UpdateGCommand"
Text="Update">
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:TextBox
ID="textBox1"
runat="server"
Text='<%#Eval("Name")%>'>
</asp:TextBox>
<asp:RequiredFieldValidator ID="rfvName" runat="server" ErrorMessage="Name is a required field"
ControlToValidate="textBox1" Text="*" ForeColor="Red">
</asp:RequiredFieldValidator>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email">
<ItemTemplate>
<asp:TextBox
ID="textBox2"
runat="server"
Text='<%#Eval("Email")%>'>
</asp:TextBox>
<asp:RequiredFieldValidator ID="rfvEmail" runat="server" ErrorMessage="Email is a required field"
ControlToValidate="textBox2" Text="*" ForeColor="Red">
</asp:RequiredFieldValidator>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Mobile">
<ItemTemplate>
<asp:TextBox
ID="textBox3"
runat="server"
Text='<%#Eval("Mobile")%>'>
</asp:TextBox>
<asp:RequiredFieldValidator ID="rfvMobile" runat="server" ErrorMessage="Mobile is a required field"
ControlToValidate="textBox3" Text="*" ForeColor="Red">
</asp:RequiredFieldValidator>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<SortedAscendingCellStyle BackColor="#FDF5AC" />
<SortedAscendingHeaderStyle BackColor="#4D0000" />
<SortedDescendingCellStyle BackColor="#FCF6C0" />
<SortedDescendingHeaderStyle BackColor="#820000" />
</asp:GridView>
在第2行中编辑电子邮件以适合电子邮件规范
点击第2行中的“更新链接”按钮后
如您所见,由于我不知道要使用什么代码,因此无法从编辑框发送正确的值。请帮忙
使用此代码。可能这就是您所需要的。
string txtName = ((TextBox) GridView4.Rows[RowIndex].FindControl("textBox1")).Text;
string txtEmail = ((TextBox) GridView4.Rows[RowIndex].FindControl("textBox2")).Text;
string txtMobile = ((TextBox) GridView4.Rows[RowIndex].FindControl("textBox3")).Text;
if (txtName != null) {
EditT.Tables[0].Rows[RowIndex]["Name"] = txtName;
}
if (txtEmail != null) {
EditT.Tables[0].Rows[RowIndex]["Email"] = txtEmail;
}
if (txtMobile != null) {
EditT.Tables[0].Rows[RowIndex]["Mobile"] = txtMobile;
}
如果有用,请对其他文本框执行相同的操作。谢谢
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句