我有一个gridview,它生成几行,每行包含几个动态创建的文本框和2个按钮。单击UpdateBtn1时,将使用文本框中的值更新数据库。最终,用户应该能够单击更新按钮或在使用任一文本框触发更新时按Enter键。
这就是我现在所处的位置。
<asp:GridView ID="InitialData" runat="server"
AutoGenerateColumns="False"
OnRowDataBound="gv_GridDataBound"
OnRowCommand="InitialRowButton_Click"
DataKeyNames="ID" OnSelectedIndexChanged="InitialData_SelectedIndexChanged">
<Columns>
<asp:TemplateField HeaderText="Update">
<ItemTemplate>
<asp:Button ID="UpdateBtn1" ButtonType="Button" Text="Update" runat="server"
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" CommandName="Run_Update" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField ReadOnly="true" HeaderText="Data" DataField="Data" />
<asp:BoundField ReadOnly="true" HeaderText="Data" DataField="Data" />
<asp:BoundField ReadOnly="true" HeaderText="Data" DataField="Data" />
<asp:BoundField ReadOnly="true" HeaderText="Data" DataField="Data" />
<asp:BoundField ReadOnly="true" HeaderText="Data" DataField="Data" />
<asp:TemplateField HeaderText="Input Textbox1">
<ItemTemplate>
<asp:Button ID="RequestBtn" CommandName="Get_Weight" HeaderText="Request Weight"
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" runat="server" Text="Request Weight" />
<asp:TextBox ID="InputText1" runat="server" Text='<%# Eval("MEASURED_WEIGHT") %>' Width="50px" />
<asp:RegularExpressionValidator ID="RegularExpWeightBox"
ControlToValidate="InputText1" runat="server" ErrorMessage="Illegal"
ValidationExpression="^[0-9]{0,5}$" class="regExpress">
</asp:RegularExpressionValidator>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:DropDownList ID="STATUSDDL" AppendDataBoundItems="True" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Input Textbox2">
<ItemTemplate>
<asp:TextBox ID="InputTextbox2" runat="server" TextMode="MultiLine" Width="200" Rows="2" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
我试图使用onclick事件来触发按钮或触发一个触发按钮的javascript函数。在这两种情况下,我都无法隔离与文本框相同的行上的按钮。任何帮助将不胜感激。
答案 0 :(得分:0)
好的,我解决了我的困境。
我最终做的是在ondatabound方法中添加一个属性来调用javascript函数。我通过找到每个控件并将其传递给属性来获取按钮Id。这是C#
protected void gv_GridDataBound(object sender, GridViewRowEventArgs e)
{
Button b_l_updateButton = (Button)e.Row.FindControl("UpdateBtn1");
e.Row.Attributes.Add("onkeydown", String.Format("RunUpdate(event,'{0}');", b_l_updateButton.ClientID));
}
这是javascript
function RunUpdate(e, UpdateButtonID) {
var keynum
if (window.event)
{
keynum = e.keyCode
}
else if (e.which)
{
keynum = e.which
}
if (keynum == 13) {
document.getElementById(UpdateButtonID).click();
}
}