我有一个带动态控件的模态弹出窗口。我需要在按钮单击中添加新文本框。
JQuery: -
<script type="text/javascript">
$(document).ready(function(){
if($('#hdnclick').val()==1){
$('#modelPopup').dialog({
autoopen:false,
title: "Add New Server",
width:650,
height:450,
modal:true,
buttons:{
Close:function(){
$(this).dialog('close');
}
}
});
$('#btnadd').click(function(){
alert('okay');
});
}
});
</script>
Aspx代码: -
<asp:GridView ID="grdservices" runat="server" AutoGenerateColumns="false" ShowFooter="true">
<Columns>
<asp:BoundField DataField="S.No" HeaderText="s.no" />
<asp:TemplateField HeaderText="service name">
<ItemTemplate>
<asp:TextBox ID="txtservicename" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="description">
<ItemTemplate>
<asp:TextBox ID="txtDescription" runat="server"></asp:TextBox>
</ItemTemplate>
<FooterStyle HorizontalAlign="right" />
<FooterTemplate>
<asp:Button ID="btnadd" runat="server" Text="add new service" OnClick="btnadd_Click" OnRowCommand="ButtonClicked" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
/asp:GridView>
我的问题是这个&#34; btnAddNewServic_Click&#34;点击按钮点击第一次点击,但这是&#34; btnAddNewServic_Click&#34;函数未在第二次单击时触发,即使在第二次单击时未触发任何内容。任何人都可以帮我恢复这个问题..
输出:
添加新行: -
protected void grdServices_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "ButtonClicked")
{
hdnclick.Value = "1";
AddNewRowToGrid();
}
}
private void AddNewRowToGrid()
{
int rowindex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
TextBox Box1 = (TextBox)grdservices.Rows[rowindex].Cells[1].FindControl("txtservicename");
TextBox Box2 = (TextBox)grdservices.Rows[rowindex].Cells[2].FindControl("txtDescription");
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["S.No"] = i + 1;
dtCurrentTable.Rows[i - 1]["Column1"] = Box1.Text;
dtCurrentTable.Rows[i - 1]["Column2"] = Box2.Text;
rowindex++;
}
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtCurrentTable;
grdservices.DataSource = dtCurrentTable;
grdservices.DataBind();
}
}
}
答案 0 :(得分:1)
使用此
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "ButtonClicked")
{
//Do Stuff
}
}
将RowCommand添加到按钮并使用上面的代码来获取&#34; ButtonClicked&#34;命令并使用您的方法
将列添加到网格视图答案 1 :(得分:0)
为按钮控件添加类名
<asp:Button ID="btnadd" runat="server" Text="add new service" OnClick="btnadd_Click" OnRowCommand="ButtonClicked" class="btnAdd" />
将此类名称用作点击事件触发器,如
$('#btnadd').click(function(){
alert('okay');
});
更改以下
$('.btnadd').click(function(){
alert('okay');
});
如果再次加载网格项,则应检查页面是否已加载。我认为一个是渲染控件的问题,因为你动态创建一个控件,所以你也应该检查一个。