簡體   English   中英

ASP.NET radiobuttonlist onclientclick

[英]ASP.NET radiobuttonlist onclientclick

我注意到ASP.NET控件集中的radiobuttonlist沒有OnClientClick()屬性。 這是微軟有目的的遺漏嗎? 無論如何,我試圖將OnClick添加到單選按鈕列表中,如下所示:

For Each li As ListItem In rblSearch.Items
    li.Attributes.Add("OnClick", "javascript:alert('jon');")
Next

但唉,它不起作用。 我甚至在firebug中檢查了源代碼,並且radiobuttonlist中沒有顯示javascript。 有誰知道如何讓這個非常簡單的工作? 我正在使用ASP.NET控件adpaters,所以不知道這是否與它有任何關系。

(我希望asp.net/javascript可以解決方案!)

我發現我可以向RadioButtonList添加一個onclick屬性,它會按預期觸發javascript客戶端。

<asp:RadioButtonList ID="RadioButtonList1" runat="server" onclick="alert('RadioButtonListSelectChange');">
    <asp:ListItem Text="One" Value="1"/>
    <asp:ListItem Text="Two" Value="2"/>
</asp:RadioButtonList>

然后,您可以編寫可以確定當前所選列表項的客戶端腳本。

不是一個答案,而是對其他建議的一些奇怪的觀察。 我將在前面說這是一個ASP.Net 4.0應用程序中的RadioButtonList,其中RepeatDirection = Vertical

1)添加onclick ='myJsFunction(this);' ListItems的屬性對我來說很好, 除非頁面上的不同項導致回發后(例如按鈕操作),ListItems上的屬性不會在回發結果中呈現 它們被渲染開始,但不是在有按鈕動作時。 去搞清楚。

2)顯然我們的遺留代碼依賴於RadioButtonList / ListItem上的一些模糊,不規范的行為,這些行為在回發時變得非常混亂。 如果在ListItem上放置id屬性,ASP.Net 2.0+將在單選按鈕周圍添加span標記並將id值放在那里。 如果你在它上面添加一個onclick屬性,它將在單選按鈕上 - 至少當Page.IsPostBack == false時。

<asp:RadioButtonList  ID="proceedChoice" runat="server" RepeatDirection="Vertical">
    <asp:ListItem id="Close_Case_Y" Value="Y" onclick="SelectResolveType(this);" Text="I want to close this case."  runat="server" />
    <asp:ListItem id="Close_Case_N" Value="N" onclick="SelectResolveType(this);" Text="I want to cancel this and start over."  runat="server" />
</asp:RadioButtonList>

呈現為

<table id="...proceedChoice" border="0">
<tr>
    <td><span id="Close_Case_Y"><input id="..._proceedChoice_0" type="radio" value="Y" onclick="SelectResolveType(this);" /><label for="..._proceedChoice_0">I want to close this case.</label></span></td>
</tr><tr>
    <td><span id="Close_Case_N"><input id="..._proceedChoice_1" type="radio" value="N" onclick="SelectResolveType(this);" /><label for="..._proceedChoice_1">I want to cancel this case and start over.</label></span></td>
</tr>
</table>

從其他按鈕回發后,雖然相同的模板呈現為

<table id="...proceedChoice" border="0">
<tr>
    <td><input id="..._proceedChoice_0" type="radio" value="Y" /><label for="..._proceedChoice_0">I want to close this case.</label></td>
</tr><tr>
    <td><input id="..._proceedChoice_1" type="radio" value="N" /><label for="..._proceedChoice_1">I want to cancel this case and start over.</label></td>
</tr>
</table>

但是在這里它變得非常奇怪:如果你將其他屬性添加到ListItem而不是id和onclick,那么保留那些屬性。 例如,如果我向其中一個ListItem添加hidden =“hidden”,它會在回發后呈現如下

<table id="...proceedChoice" border="0">
<tr>
    <td><span hidden="hidden"><input id="..._proceedChoice_0" type="radio" value="Y" /><label for="..._proceedChoice_0">I want to close this case.</label></span></td>
</tr><tr>
    <td><input id="..._proceedChoice_1" type="radio" value="N" /><label for="..._proceedChoice_1">I want to cancel this case and start over.</label></td>
</tr>
</table>

onLlick屬性在ListItem對象中非常好用。 試試這種方式:

li.Attributes.Add("onclick", "javascript:showDiv()")

那個功能對我來說。

            foreach (PlaceType t in query.ToList())
        {

            ListItem li = new ListItem(@"<img src=""" + t.ImageRelativePath + @"""/><br/>" + t.PlaceTypeText, t.PlaceTypeID.ToString());
            li.Attributes.Add("onclick", "javascript:placeTypeChange('" + t.ImageRelativePath + "')");
            rblPlaceTypes.Items.Add(li);
        }

所以我在那里

a)迭代EF查詢的結果。 b)創建一個新的列表項,並將其添加到我的單選按鈕列表(rblPlaceTypes)。 c)構造函數正在放入一個圖像以便顯示。 d)li.Attributs.Add正在放入javascript連線。

因為它是一個列表控件,所以沒有OnClientClick事件。 使用回發(SelectedIndexChange)或編寫javascript來獲取每個單選按鈕的單擊。

暫無
暫無

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

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