我有一个列表视图,它显示了数据库中的数据列表,并在每一行中提供了一个按钮,其中使用命令参数在后端检索id,并且相应ID的数据显示在网格视图。 代码如下:
<asp:ListView ID="lstvwResultInquiry" runat="server" DataKeyNames="inquiry_id"
onitemdatabound="ListView1_ItemDataBound"
onpagepropertieschanging="PagePropertiesChanging"
onitemcanceling="CancelListViewItem" onitemediting="EditListViewItem"
onselectedindexchanging="lstvwResultInquiry_SelectedIndexChanging" >
<LayoutTemplate >
<div id="rightnowIn">
<table class="gridview" cellpadding="5" cellspacing="5" >
<tr class="header">
<th width="140">Company</th>
<th width="220">Event</th>
<th width="125">Country</th>
<th width="125">Date</th>
<th width="100">Details</th>
</tr>
<tr id="itemPlaceholder" runat="server" />
<tr id="Tr1" runat="server" align="center" >
<td colspan="2" align="left"><asp:Label ID="lblCount" runat="server"></asp:Label></td>
<td id="Td1" runat="server" style="" colspan="4">
<asp:DataPager class="mpart" ID="DataPager1" PageSize="10" runat="server" align="center">
<Fields>
<asp:NumericPagerField ButtonType="Link" ButtonCount="3" PreviousPageText="<<<" NextPageText=">>>" />
</Fields>
</asp:DataPager>
</td>
<td colspan="2" align="left"></td>
</tr>
</table>
</div>
</LayoutTemplate>
<ItemTemplate >
<tr class="itemTemplateTr" >
<td style="color: #403f3f; font-weight: bold;"><%# Eval("company")%></td>
<td><asp:Label ID="lblEvents" runat="server" ></asp:Label></td>
<td><%# Eval("country_name")%></td>
<td><%# Eval("date", "{0:dd-MM-yyyy}")%></td>
<% if (Session["session_log_id"] != null)
{ %>
<%--<td class="command"><asp:HyperLink ID="btnEdit" runat="server" Text="View" OnClientClick="javascript: show_popup()" CommandArgument='<%#Eval("inquiry_id")%>' CommandName="view" cssclass="linkEditButton" Font-Underline="True" /></td>--%>
<td> <asp:Button ID="Button2" runat="server" Text="view" OnClientClick="javascript: show_popup()" CommandName="view" CommandArgument='<%# Eval("inquiry_id")%>' OnClick="buttonClick"/> </td>
<%} %>
<td>
<asp:Button ID="buttReminder" runat="server" Text="view" Visible="false" CssClass="reminderButton" CommandArgument='<%# Eval("inquiry_id")%>' OnClick="buttReminderInquiry_Click" BorderStyle="None" /></td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr class="alternatingItem">
<td style="color: #403f3f; font-weight: bold;"><%# Eval("company")%></td>
<td><asp:Label ID="lblEvents" runat="server" ></asp:Label></td>
<td><%# Eval("country_name")%></td>
<td><%# Eval("date", "{0:dd-MM-yyyy}")%></td>
<% if (Session["session_log_id"] != null)
{ %>
<%--<td class="command"><asp:HyperLink ID="btnEdit" runat="server" Text="View" OnClientClick="javascript: show_popup()" CommandArgument='<%# Eval("inquiry_id")%>' cssclass="linkEditButton" Font-Underline="True" /></td> --%>
<td> <asp:Button ID="Button2" runat="server" Text="view" CommandName="view" CommandArgument='<%# Eval("inquiry_id")%>' OnClick="buttonClick" OnClientClick="javascript: show_popup()" /></td>
<%} %>
<td>
<asp:Button ID="buttReminder" runat="server" Visible="false" CssClass="reminderButton" CommandArgument='<%# Eval("inquiry_id")%>' OnClientClick="javascript: show_div()" BorderStyle="None" /></td>
</tr>
</AlternatingItemTemplate>
<EmptyDataTemplate>
<div style="text-align:center;font-weight:bold">0 Results Founds</div>
</EmptyDataTemplate>
</asp:ListView>
单击视图按钮的代码如下所示,它绑定网格视图的数据,但数据不显示在网格视图中:
Button btn = (Button)sender;
string CommandName = btn.CommandName;
string inquiryID = btn.CommandArgument;
id = Convert.ToInt32(btn.CommandArgument.ToString());
this.BindGrid();
但是,如果我使用其他按钮尝试相同的代码而不是它的工作。它不能使用列表视图按钮单击..请帮助
答案 0 :(得分:0)
按钮单击无效,因为动态创建的按钮没有与之关联的点击事件。做这样的事情。
Button btn = (Button)sender;
string CommandName = btn.CommandName;
string inquiryID = btn.CommandArgument;
id = Convert.ToInt32(btn.CommandArgument.ToString());
btn.Click += btn_Click;
和“btn.Click + = btn_Click;”这条线用于事件处理。 &安培;在其中编写绑定代码
private void button_Click(object sender, EventArgs e)
{
//binding code goes here
}
尝试这样的事情(未经测试)
AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
trigger.ControlID = btn.UniqueID;
trigger.EventName = "Click";
yourUpdPanel.Triggers.Add(trigger);
ScriptManager scriptManager = ScriptManager.GetCurrent(Page);
if (scriptManager != null) {
scriptManager.RegisterAsyncPostBackControl(btn);
}