如何在客户端判断gridview中单选是否被选中?
gridview有一列是radio单选按纽,我是为做修改删除用的,目前是我在button执行的时候检查是否有radio被选中,这样效率比较低,想问如何在客户端上判断是否有被选中的radio呢,加一个Attributes( "onClick ") 事件的话,又怎么保证如果没选中的话停止执行服务端的修改删除操作.
------解决方案--------------------var grd = document.getElementById( ' <% =GridView1.ClientID %> ');
var radioArr = grd.getElementsByTagName( "input ");
for(var i = 0; i < radioArr.length; i++) {
if(radioArr.type == "radio " && radioArr[i].checked) { // && radioArr.id.indexOf(...) > -1
// .....
}
}
------解决方案--------------------参考使用Javascript选择Gridview行的源代码
http://bbs.msproject.cn/default.aspx?g=posts&t=43
------解决方案--------------------function CheckAllDataGridCheckBoxes(aspCheckBoxID, checkVal)
{
isSelectAllSelected = checkVal;
//generated control name starts with a colon
re = new RegExp(aspCheckBoxID);
for(i = 0; i < document.forms[0].elements.length; i++)
{
elm = document.forms[0].elements[i];
if (elm.type == 'checkbox ')
{
if (re.test(elm.name))
{
if(elm.disabled)
{
continue;
}
elm.checked = checkVal;
}
}
}
}
------解决方案--------------------javascript:
function hasRadioChecked()//是否有选中
{
var grd = document.getElementById( ' <% =GridView1.ClientID %> ');
var arr = grd.getElementsByTagName( "input ");
for(var i = 0; i < arr.length; i++)
{
if(arr[i].type == "radio " && arr[i].checked) { return true; }
}
alert( "请选择要删除的项! ");
return false;
}
function radioClick(clientid)
{
var grd = document.getElementById( ' <% =GridView1.ClientID %> ');
var arr = grd.getElementsByTagName( "input ");
for(var i = 0; i < arr.length; i++)
{
if(arr[i].type == "radio ")
{ arr[i].checked = false;}
}
document.getElementById(clientid).checked = true;
}
html
<asp:GridView ID= "GridView1 " ....>
<Columns>
<asp:TemplateField>
<ItemTemplate> <asp:RadioButton ID= "rbDetail " runat= "server " /> </ItemTemplate>
</asp:TemplateField>
.....
<Columns>
</asp:GridView >
<asp:Button ID= "deleteBtn " OnClientClick= "javascript:return hasRadioChecked(); " runat= "server "/>
后台:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)//给GridView控件上面的RadioButton加载点击属性
{
System.Web.UI.WebControls.RadioButton radio = (System.Web.UI.WebControls.RadioButton)e.Row.FindControl( "rbDetail ");
radio.Attributes.Add( "onclick ", "javascript:radioClick( ' " + e.Row.FindControl( "rbDetail ").ClientID + " '); ");
}
}
------解决方案--------------------