tags:

views:

321

answers:

2

Hi, 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.GetType(), "alertmessage",
                       "<script>alert('Select a course above first');</script>");
    }
}

Thanks.

A: 

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 = "";
}
Kobi
A: 

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
   }
}
peacmaker