簡體   English   中英

在asp上檢查單選按鈕的狀態:鏈接按鈕回發

[英]radiobutton checked status on asp:linkbutton postback

我在中繼器控件中有四個單選按鈕,該控件本身在更新面板中

//代碼是這樣的

`<asp:update panel ..>
...
<asp:Repeater>
..
<asp:checkbox>
..
..
</asp:update panel ..>
<asp:LinkButton ID="next2" runat="server" CssClass="button_Submit" Font-Bold="true"    OnClick="next_ServerClick" Text="Submit"> 
<asp:ImageButton ID="next" ImageUrl="~/images/newSummary.jpg" runat="server" OnClick="next_ServerClick" ImageAlign="Middle"/>
protected void next_ServerClick(object sender, EventArgs e)
{
foreach (System.Web.UI.WebControls.RepeaterItem Item in repeatercontrol.Items)
{
   chkbox = ((CheckBox)Item.FindControl(chkboxName));
   if (chkbox.checked)
   {
    ...
   }
}
}`

我選擇其中一個復選框,當我單擊“圖像”按鈕時,便能夠獲得正確的狀態(選中= true)。

但是,當我使用鏈接按鈕時,它總是以選中= false的形式出現,好像選擇沒有注冊。

為什么會這樣?

您需要在更新面板中將linkbutton click事件注冊為觸發器。 請參見下面的示例:

<asp:UpdatePanel ID="UpdatePanel1" runat="server" >
    <ContentTemplate>

        ...

    </ContentTemplate>

    <Triggers>

        <asp:AsyncPostBackTrigger ControlID="LinkButtonID" EventName="Click" />

    </Triggers>

</asp:UpdatePanel>

如果看不到代碼,則控件將不正確的唯一原因是,如果在加載ViewState之前在頁面生命周期中對其進行檢查。

編輯:使用單獨的事件聲明並集中查找邏輯:

    protected void LinkButton1_Click(object sender, EventArgs e)
    {

    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {

    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM