Disable Linkbutton from Client-Side Javascript - help!

J

James Radke

Hello,

I have a datagrid, which contains data, as well as a linkbutton column and a
button column. I have added attributes to the button like:

button.attributes.add("OnClick", "javascript: funcname('" &
linkbutton.UniqueID & "');")

*NOTE: this code is taking place in the OnItemDataBound function, and
after I have the valid reference to the correct linkbutton......

This should then run the client side javascript function as follows:

function funca(linkID) {
document.getElementById(linkID).disabled = false
}

For all other controls, and even standard buttons, the getElementById
function works perfectly like this. However, for the linkbutton, it does
not return an object so I cannot disable the linkbutton from the client side
code.

Can someone tell me how I can accomplish this?

Why doesn't the above code work?

Thanks!

Jim
 
J

Jeffrey Tan[MSFT]

Hi

Thank you for using MSDN Newsgroup! My name is Jeffrey, and I will be
assisting you on this issue.

Based on my understanding, you want to add some OnClick client javascript
handler code to your linkbutton column of datagrid control at server side,
but it does not work.

==========================================================================
Actually, LinkButton control will generate as <a> html tag at client, and
whenever you click the linkbutton, your page will postback to server side.

Based on my research, I suspect that the problem may occur:
1). Your javascript code works, but the page postback to server side, and
the linkbutton re-initialized with its original state.
2). You should use LinkButton's ClientID property not UniqueID:
lb.Attributes.Add("OnClick","javascript: funcname('"+ lb.ClientID + "');");
3). Default, the datagrid will not render the client id for linkbutton
column, so you should render it yourself(Hook into ItemCreate event and set
the ID for your linkbutton)

To determine 1), you can place alert method in your javascript code to
prove that your html setting really take effect, like this:
<script language="javascript">
function funcname(linkID)
{
alert(document.all(linkID).innerText);
document.getElementById(linkID).innerText="worked!";
alert(document.all(linkID).innerText);
}
</script>

For 3), If you do not databind your datagrid when postback in Page_Load, I
think you should add the ID in ItemCreate event(Because ItemDataBound event
only fires when databind)
Full sample code like this:

private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
SqlDataAdapter adapter=new SqlDataAdapter("select * from
jobs","server=localhost;database=pubs;uid=sa;pwd=");
DataSet ds=new DataSet();
adapter.Fill(ds);

DataGrid1.DataSource=ds;
DataGrid1.DataBind();
}
}

private void DataGrid1_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType==ListItemType.AlternatingItem)
{
DataGridItem dgi=e.Item;
foreach(Control c in dgi.Cells[0].Controls)
{
if(c is System.Web.UI.WebControls.LinkButton)
{
LinkButton lb=(LinkButton)c;
lb.Attributes.Add("OnClick","javascript: funcname('"+ lb.ClientID +
"');");

}
}
}
}

private void DataGrid1_ItemCreated(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType==ListItemType.AlternatingItem)
{
DataGridItem dgi=e.Item;
foreach(Control c in dgi.Cells[0].Controls)
{
if(c is System.Web.UI.WebControls.LinkButton)
{
LinkButton lb=(LinkButton)c;
lb.ID="lb";
}
}
}
}

===========================================================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top