目前我在更新 DataGrid 功能时遇到问题。它无法捕获我在 DataGrid 中的输入。有没有办法通过使用 DataGrid 来捕获我的输入?因为互联网上大部分信息使用的都是不同的GridView。
已编辑:这是一个具有更新、编辑和删除功能的 DataGrid。例如,我选择一行数据并按编辑按钮,在所选行上填写数据,然后按更新按钮更新所选行信息。在这种情况下,我无法在编辑后捕获选定的行信息。所以,我无法更新行数据。
这是 HTML 的代码
<asp:DataGrid ID="dgRecords" runat="server" Width="100%" DataKeyField="InsitePriceID"
AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" CellPadding="5"
OnPageIndexChanged="dgRecords_PageIndexChanged" OnSortCommand="dgRecords_SortCommand"
OnDeleteCommand="dgRecords_OnDelete" OnEditCommand="dgRecords_OnEdit" OnUpdateCommand="dgRecords_OnUpdate"
OnCancelCommand="dgRecords_OnCancel">
<AlternatingItemStyle CssClass="Data"></AlternatingItemStyle>
<ItemStyle CssClass="Data"></ItemStyle>
<HeaderStyle CssClass="ColHeader"></HeaderStyle>
<Columns>
<asp:BoundColumn Visible="False" DataField="InsitePriceID"></asp:BoundColumn>
<asp:BoundColumn DataField="ServicePartFrom" HeaderText="No. of Service Part (From)"
SortExpression="ServicePartFrom"></asp:BoundColumn>
<asp:BoundColumn DataField="ServicePartTo" HeaderText="No. of Service Part (To)"
SortExpression="ServicePartTo"></asp:BoundColumn>
<asp:BoundColumn DataField="BaseAmount" HeaderText="% from Base Amount" SortExpression="BaseAmount">
</asp:BoundColumn>
<asp:EditCommandColumn ButtonType="PushButton" CancelText="Cancel" EditText="Edit"
UpdateText="Update"></asp:EditCommandColumn>
<asp:ButtonColumn ButtonType="PushButton" CommandName="Delete" Text="Delete"></asp:ButtonColumn>
</Columns>
<PagerStyle Mode="NumericPages"></PagerStyle>
</asp:DataGrid>
<td style="width: 1014px">
<asp:Label ID="lbla" runat="server"></asp:Label>
<asp:Label ID="lblb" runat="server"></asp:Label>
<asp:Label ID="lblc" runat="server"></asp:Label>
</td>
后面的代码。
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ucWScr.SetValue("Insite Price");
pnlMsg.Visible = false;
BindData("");
}
}
private void BindData(string _sortExpression)
{
clsAdmin obj = new clsAdmin();
int intNoRec = 0;
DataTable tbl = obj.SearchInsitePrice();
}
protected void dgRecords_OnUpdate(object source, DataGridCommandEventArgs e)
{
if (e.CommandName == "Update")
{
string strInsitePriceID = dgRecords.DataKeys[e.Item.ItemIndex].ToString();
//Error start from here
lbla.Text = e.Item.Cells[1].Text;
lblb.Text = e.Item.Cells[2].Text;
lblc.Text = e.Item.Cells[3].Text;
int intServicePartFrm = Int32.Parse(lbla.Text);
int intServicePartTo = Int32.Parse(lblb.Text);
int fltPercentBaseAmt = Int32.Parse(lblc.Text);
//Error ends here
string strUserLogin = CurrentUser.UserLogin.ToString();
DateTime dteNow = DateTime.Now;
if (strInsitePriceID != "")
{
EStatus status = webform.UpdateInsitePrice(strInsitePriceID, intServicePartFrm, intServicePartTo, fltPercentBaseAmt, strUserLogin, dteNow);
if (status != EStatus.Success) throw new Exception("Update failed.");