开发者

asp.net Gridview check selected index client side

开发者 https://www.devze.com 2022-12-14 18:02 出处:网络
how can i do this client side, OnClientClick of the btnYes will return false if the gridview selected index < 0;

how can i do this client side, OnClientClick of the btnYes will return false if the gridview selected index < 0;

protected void btnYes_Click(object sender, EventArgs e)
{
    if (gvCourseDetails.SelectedIndex == -1)
    {
        ClientScriptManager scriptManager = Page.ClientScript;
        scriptManager.RegisterClientScriptBlock(this.Ge开发者_JAVA技巧tType(), "alertmessage",
                       "<script>alert('Select a course above first');</script>");
    }
}

Thanks.


The GridView's selected index will not change without a postback anyway, so you can evaluate it only once on the server side (on page_load or btnYes prerender, etc). It is almost meaningless on the client side.

if (gvCourseDetails.SelectedIndex == -1)
{
   btnYes.OnClientClick = "alert('Select a course above first'); return false;";
}
else
{
   btnYes.OnClientClick = "";
}


i think you can declare a public variable and store the SelectedIndex in it then call it from the client side script but you will need to post back the page:

public int selectedIndex = 0;
protected void Page_Load(object sender, EventArgs e)
{
selectedIndex = gvCourseDetails.SelectedIndex;
}

//and here the client side script

function CheckSelectedIndex()
{
   if(<%= selectedIndex%> == -1)
   {
       // type you code
   }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号