[英]Get value from Gridview for update
我有這個Gridview與AutoGenerateEditButton =“true”:
<asp:GridView ID="myGridview" runat="server"
ShowFooter="True"
AutoGenerateDeleteButton="true" AutoGenerateEditButton="true"
AutoGenerateColumns="False" AllowSorting="True" DataKeyNames="id" DataSourceID="odsVolumeSearch" OnRowUpdating="myGridview_RowUpdating">
<Columns>
<asp:BoundField DataField="id" HeaderText="id" Visible="false" InsertVisible="False" ReadOnly="True" SortExpression="id" />
<asp:BoundField DataField="Date" HeaderText="date" SortExpression="Date" ReadOnly="True" />
<asp:BoundField DataField="Items" HeaderText="Items" SortExpression="Items" />
</Columns>
</asp:GridView>
這是我的objectDataSource:
<asp:ObjectDataSource ID="myOds" runat="server"
DeleteMethod="myDeleteMethod" SelectMethod="mySelectMethod"
TypeName="TheirLocation.sqlDataLayer" UpdateMethod="myUpdateMethod">
<DeleteParameters>
<asp:Parameter Name="id" Type="Int32" />
</DeleteParameters>
<SelectParameters>
<asp:Parameter Name="fromDate" Type="DateTime"/>
<asp:Parameter Name="toDate" Type="DateTime"/>
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="id" Type="Int32" />
<asp:Parameter Name="volume" Type="Int32" />
</UpdateParameters>
</asp:ObjectDataSource>
這里的混亂是我的更新事件處理程序:
protected void gvVolumeList_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = gvVolumeList.Rows[e.RowIndex];
String debugString = ((TextBox)(row.Cells[1].Controls[0])).Text;
Response.Write("<script>alert('" + debugString + "');</script>");
}
我只是試圖從我的文本框中獲取值以顯示警報,但我無法修復它。 我嘗試了各種各樣的東西,並像瘋了似的google但我無法獲得價值
編輯
我認為問題是我從CELL獲取文本,而不是單元格中的文本框。 仍然不知道該怎么做
如果你想從你的gridview獲得Id! 如果您已將Bound字段可見性聲明為false,則不會呈現您的綁定字段,因此您無法通過使用獲取其值
String debugString = ((TextBox)(row.Cells[1].Controls[0])).Text;
你的單元格索引從0開始而不是從1開始(如果你想獲得Id)。 更好地使用gridview的RowCommand或者讓你的Id屬性visible ="true"
- - - - - - - - - - - - - 要么 - - - - - - - - - - - - -----使用模板字段
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="HiddenField1" runat="server"
Value='<%# Eval("Id") %>' />
....
</ItemTemplate>
代碼背后
if (row.RowType == DataControlRowType.DataRow)
{
HiddenField Id = (HiddenField)row.Cells[0].FindControl("HiddenField1");
}
protected void GridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = TaskGridView.Rows[e.RowIndex];
String str = ((TextBox)(row.Cells[2].Controls[0])).Text;
}
這是如何在行Update中獲取控件值。
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
//int id = Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
TextBox tname = (TextBox)row.FindControl("nam");
//to get value of first cell
string str = row.Cells[0].Text.ToString();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.