![](/img/trans.png)
[英]asp:ImageButton is not firing in an asp.net UpdatePanel?
[英]ImageButton In UpdatePanel Doesn't Firing [Asp.net]
我在 asp.net 更新面板中单击按钮时遇到问题。 这是我的代码:
我的 Aspx 代码:
<asp:UpdatePanel runat="server" ID="UpdatePanel1" UpdateMode="Conditional">
<ContentTemplate>
<div class="modal fade modal-lg" id="mdlVendor" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="mdlVendorLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="mdlVendorLabel">Vendor</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<div class="row">
<div class="col-md-3">
<asp:Label runat="server" ></asp:Label>
<asp:TextBox runat="server" ID="txtVendorSearch" CssClass="form-control me-2" OnClientClick="javascript:return false;" data-target="#mdlVendor" OnTextChanged="txtVendorSearch_TextChanged" AutoPostBack="true"></asp:TextBox>
<br />
</div>
<div class="col-md-3">
</div>
</div>
<asp:GridView ID="gvVendor" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="false" Width="100%" OnRowCommand="gvVendor_RowCommand">
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
<EmptyDataTemplate>
<asp:Label runat="server" ID="lblEmptyData" Text="No Data Found"></asp:Label>
</EmptyDataTemplate>
<Columns>
<asp:TemplateField HeaderText="Vendor Id">
<ItemTemplate>
<asp:Label runat="server" ID="VendorID" Text='<%# Eval("VendorID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Vendor Name">
<ItemTemplate>
<asp:Label runat="server" ID="VendorName" Text='<%# Eval("VendorName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ImageUrl="~/img/add.png" runat="server" Width="20px" Height="20px" CommandName="SelectDataVendor" ToolTip="Select Data Vendor"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</ContentTemplate>
</asp:UpdatePanel>
我的 Aspx.cs 代码:
protected void gvVendor_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("SelectDataVendor"))
{
GridViewRow row = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);
int index = row.RowIndex;
GridViewRow clickedRow = gvVendor.Rows[index];
Label VendorName = (Label)clickedRow.Cells[2].FindControl("VendorName");
txtVendor.Text = VendorName.Text;
}
}
protected void txtVendorSearch_TextChanged(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "openModal();", true);
DataTable dt = new DataTable();
string StrSeacrh = txtVendorSearch.Text;
dt = trxExpenditureCls.GetVendor(StrSeacrh);
if (dt.Rows.Count > 0)
{
gvVendor.DataSource = dt;
gvVendor.DataBind();
}
else
{
dt.Rows.Add(dt.NewRow());
gvVendor.DataSource = dt;
gvVendor.DataBind();
gvVendor.Rows[0].Cells.Clear();
gvVendor.Rows[0].Cells.Add(new TableCell());
gvVendor.Rows[0].Cells[0].ColumnSpan = dt.Columns.Count;
gvVendor.Rows[0].Cells[0].Text = "No Data Found";
gvVendor.Rows[0].Cells[0].HorizontalAlign = HorizontalAlign.Center;
}
}
当我单击 imageButton 时:
<asp:ImageButton ImageUrl="~/img/add.png" runat="server" Width="20px" Height="20px" CommandName="SelectDataVendor" ToolTip="Select Data Vendor"/>
系统不会调试到 gvVendor_RowCommand。 当我在 Page_Load 中设置断点时,调试器也会进入方法 gvVendor_RowCommand。 但是我遇到了一个问题,调试器完成后 txtVendor 没有填充。
我已经在尝试:
<asp:UpdatePanel runat="server" ID="updPnl2" UpdateMode="Conditional">
<ContentTemplate>
<asp:ImageButton ImageUrl="~/img/add.png" runat="server" Width="20px" Height="20px" CommandName="SelectDataVendor" ToolTip="Select Data Vendor" ID="btnAdd"/>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger controlid="btnAdd" eventname="Click" />
</Triggers>
</asp:UpdatePanel>
而且两者
<asp:AsyncPostBackTrigger controlid="btnAdd" eventname="Click" />
或者
<asp:PostBackTrigger controlid="btnAdd" />
没有一个可以解决我的问题,即使我已经尝试过:
<asp:UpdatePanel runat="server" ID="updPnl2" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button runat="server" ID="btnAddNewData" OnClick="bntAddNewData_Click" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger controlid="btnAddNewData" eventname="Click" />
</Triggers>
</asp:UpdatePanel>
还是没有解决我的问题,有谁能帮帮我吗? 我被困在这里。 之前谢谢你
嗯,我认为更新面板的引入并不重要。
另一方面? 我很久以前就放弃了 row 命令的使用。
大多数人没有意识到您可以放入标准的 asp.net 按钮或图像按钮。
只需为后面的代码连接一个平面简点击事件。 这种方法的美妙之处在于您不必将一堆事件混杂到行命令中。 (很少有理由打扰或使用它)。
因此,假设我们将一个按钮放入 GV。
现在,我们不能双击设计器中的按钮来自动连接事件,但您可以在标记中执行此操作。 只需输入 onclick= -- intel-sense 就会弹出这个:
所以,选择创建新事件。
现在您有了图像按钮单击事件的代码:
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
ImageButton btn = (ImageButton)sender;
GridViewRow gRow = (GridViewRow)btn.NamingContainer;
// get pkID
int PKID = (int)GridView1.DataKeys[gRow.RowIndex]["ID"];
Debug.Print("PK ID = " + PKID);
Debug.Print("Row index click = " + gRow.RowIndex);
Label VendorName = (Label)gRow.FindControl("VendorName");
txtVendor.Text = VendorName.Text;
}
所以你不必使用 row 命令。 但是,您可以放入一个普通的简按钮或图像按钮 - 只需连接一个普通的旧常规事件。 您可以按照上述方式选择当前行,然后参加比赛。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.