[英]Why does the first click on a radio button in a gridview do nothing?
I have a gridview that has a column of radio buttons. 我有一个gridview,有一列单选按钮。 I have a javascript function that only allows the user to click on a single radio button.
我有一个javascript函数,只允许用户点击一个单选按钮。 The first time you click on the gridview nothing happens.
第一次单击gridview时没有任何反应。 Every click after that works fine.
之后的每次点击都可以。
Update: If i click directly on the radio button, it does work the first time but if i click on the row, I have to click twice. 更新:如果我直接点击单选按钮,它第一次工作,但如果我点击该行,我必须点击两次。
<script type="text/javascript">
function SelectSingleRadiobutton(rdbtnid) {
var rdBtn = document.getElementById(rdbtnid);
var rdBtnList = document.getElementsByTagName("rbTypeGroup");
for (i = 0; i < rdBtnList.length; i++) {
if (rdBtnList[i].type == "radio" && rdBtnList[i].id != rdBtn.id) {
rdBtnList[i].checked = false;
}
}
}
</script>
<asp:GridView ShowHeader="false" SkinID="noborder" runat="server" ID="gvType" AutoGenerateColumns="false"
OnRowDataBound="gvType_RowDataBound" ClientIdMode="Predictable">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:RadioButton ID="optValue" runat="server" Name="options" Style="cursor: default;" Tag="rbTypeGroup" OnClick="SelectSingleRadiobutton(this.id)"/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Name" />
<asp:BoundField DataField="Description" />
</Columns>
</asp:GridView>
protected void gvType_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
HibConfig.IncidentType type = (HibConfig.IncidentType)e.Row.DataItem;
RadioButton rdo = ((RadioButton)e.Row.FindControl("optValue"));
rdo.Checked = incident.Type.Id == type.Id ? true : false;
e.Row.Attributes.Add("onclick", string.Format("document.getElementById('{0}').click();", rdo.ClientID));
e.Row.Attributes.Add("dataId", type.Id.ToString());
}
}
The grid view may be getting loaded after your JS is being initialized. 在初始化JS之后,可能会加载网格视图。 I think what you are seeing is on the initial page load your js does not have the context of the gridview.
我认为你所看到的是在初始页面加载你的js没有gridview的上下文。 Your initial click of the grid row causes a postback which then loads the JS file.
您初始点击网格行会导致回发,然后加载JS文件。 If you are using Jquery you may want to wrap the JS in a
document.ready()
or $function() call
. 如果您正在使用Jquery,您可能希望将JS包装在
document.ready()
或$function() call
。 Another option would be to try and load the JS at the bottom of the document to try to ensure that the DOM has rendered. 另一种选择是尝试在文档底部加载JS以尝试确保DOM已呈现。 I've had a number of JS files "break" or loose context of the page with asp elements.
我有一些JS文件“破解”或松散的页面上下文与asp元素。 Postbacks can cause a number of issues with JS
回发可能会导致JS出现许多问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.