如题,我在GridView中的模板列中定义了一个TextBox(GridView有多列数据),然后在GridView控件外面定义一个Button, 我要实现的功能是,我在GirdView模板列中的TextBox中更改了数据,然后点击Button后,得到刚才GridView模板列中更改数据的那一列的主键ID值,代码如下,谢谢高手的指点! 代码:
<asp:GridView ID="gvwOrder" runat="server"
AutoGenerateColumns="False" DataKeyNames="ProductID"
OnRowDeleting="gvwOrder_RowDeleting" Width="100%"
OnRowCommand="gvwOrder_RowCommand">
<Columns>
<asp:BoundField DataField="ProductName" HeaderText="Name" ReadOnly="True" />
<asp:BoundField DataField="UnitPrice" HeaderText="Price" ReadOnly="True"/>
<asp:TemplateField HeaderText="Qty">
<ItemTemplate>
<asp:TextBox ID="editQuantity" Width="24px" MaxLength="3" runat="server" Text='<%# Bind("Quantity") %>' ></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="SalesAmount" HeaderText="Amt" ReadOnly="True" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>
其中,<asp:TextBox ID="editQuantity"是我在上面更改数据,<asp:Button ID="btnUpdateQuantity" runat="server" Text="Update Qty" OnClick="btnUpdateQuantity_Click" />按钮<asp:Button ID="btnUpdateQuantity" 是我点击后,要在点击事件方法中找到GridView模板列中更改了TextBox内容所对应的主键ID值。希望高手能赐教!!!
<asp:GridView ID="gvwOrder" runat="server"
AutoGenerateColumns="False" DataKeyNames="ProductID"
OnRowDeleting="gvwOrder_RowDeleting" Width="100%"
OnRowCommand="gvwOrder_RowCommand">
<Columns>
<asp:BoundField DataField="ProductName" HeaderText="Name" ReadOnly="True" />
<asp:BoundField DataField="UnitPrice" HeaderText="Price" ReadOnly="True"/>
<asp:TemplateField HeaderText="Qty">
<ItemTemplate>
<asp:TextBox ID="editQuantity" Width="24px" MaxLength="3" runat="server" Text='<%# Bind("Quantity") %>' ></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="SalesAmount" HeaderText="Amt" ReadOnly="True" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>
其中,<asp:TextBox ID="editQuantity"是我在上面更改数据,<asp:Button ID="btnUpdateQuantity" runat="server" Text="Update Qty" OnClick="btnUpdateQuantity_Click" />按钮<asp:Button ID="btnUpdateQuantity" 是我点击后,要在点击事件方法中找到GridView模板列中更改了TextBox内容所对应的主键ID值。希望高手能赐教!!!
解决方案 »
- asp.net绘图,,如何从存储图片的页面跳到母页面
- 静态页跟ajax的选择
- 读数据库字段值并输出到网页时出错!! 哭
- 为什么window.location没起作用?
- asp.net中如何调整用户控件的高度和宽度呢?
- 在asp代码中怎样隐藏数据库密码 线等!!!!!!!!!!
- 怎样让MainFrame跟随leftFrame刷新--框架问题
- 我想买一本关于ASP.net入门的书,可是这类的书这么多,真不知道选哪本.
- 查询日期问题
- 为何在windows应用中能调用类方法实现打印报表功能,而在web应用中却不能?????请高手赐教。。。。。。。。。。
- 关于asp.net开发 MVC Html.RenderDynamicEntity()
- 页面中button的OnClick前的confirm对话框???
AutoGenerateColumns="False" DataKeyNames="ProductID"
OnRowDeleting="gvwOrder_RowDeleting" Width="100%"
OnRowCommand="gvwOrder_RowCommand">
<Columns>
<asp:BoundField DataField="ProductName" HeaderText="Name" ReadOnly="True" />
<asp:BoundField DataField="UnitPrice" HeaderText="Price" ReadOnly="True"/>
<asp:TemplateField HeaderText="Qty">
<ItemTemplate>
<asp:Label ID="lblQuantity" runat="server" Text=' <%# Eval("Quantity") %>' Visible="false"></asp:Label>
<asp:TextBox ID="editQuantity" Width="24px" MaxLength="3" runat="server" Text=' <%# Eval("Quantity") %>' > </asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="SalesAmount" HeaderText="Amt" ReadOnly="True" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>
<asp:Button ID="btnUpdateQuantity" runat="server" Text="Update Qty" OnClick="btnUpdateQuantity_Click" /> protected void btnUpdateQuantity_Click(object sender, EventArgs e)
{
Label lblQuantity = null;
TextBox editQuantity = null; foreach (GridViewRow row in gvwOrder.Rows)
{
lblQuantity = row.FindControl("lblQuantity") as Label;
editQuantity = row.FindControl("editQuantity") as TextBox;
if (string.Compare(lblQuantity.Text, editQuantity.Text, true) != 0)
{
int productID = Convert.ToInt32(gvwOrder.DataKeys[row.RowIndex].Value);
//做你的更新操作
}
}
}
点击页面的Button时,取ViewState["myid"]的值(取Label的值)
DataID='<%# Eval("primaryKeyField") %>' OnTextChanged="editQuantity_TextChanged"> </asp:TextBox>
之后,在事件处理程序editQuantity_TextChanged中,可以这样得到绑定的数据:protected void editQuantity_TextChanged(object sender, EventArgs e)
{
TextBox txt=sender as TextBox;
string DataID=txt.Attributes["DataID"];
//将txt.Text的值写入DataID所标记的对象持久化机制中
}如果另外一个模板列中有其它控件照样可以这样取得同一行中其它控件的值:protected void editQuantity_TextChanged(object sender, EventArgs e)
{
TextBox txt=sender as TextBox;
string DataID=txt.Attributes["DataID"];
TextBox otherTextBox=txt.FindControl("OtherTextBox1") as TextBox;
Label otherLabel=txt.FindControl("OtherLabel1") as Label;
Label otherDropDownList=txt.FindControl("OtherDropDownList1") as DropDownList;
//将txt.Text、otherTextBox.Text、otherLabel.Text、otherDropDownList.SelectedValue 的值写入DataID所标记的对象持久化机制中
}