Gridview access from Javascript

M

marksommerville

I have a few questions regarding accessing a gridview control from
Javascript which I would really appreciate some help with.

1. The gridview contains a column of checkboxes as a template field. I
have onclick Javascript on the checkbox but can't figure out how to
determine the row number of the checkbox that was clicked.

2. How do I access a control within the gridview's pagertemplate? The
pagertemplate contains a 1 row table with a button in it that I need
to enable/disable.

Thanks.
 
M

marss

I have a few questions regarding accessing a gridview control from
Javascript which I would really appreciate some help with.

1. The gridview contains a column of checkboxes as a template field. I
have onclick Javascript on the checkbox but can't figure out how to
determine the row number of the checkbox that was clicked.

2. How do I access a control within the gridview's pagertemplate? The
pagertemplate contains a 1 row table with a button in it that I need
to enable/disable.

Thanks.

Regarding for your first question: you can attach client-side script
within RowDataBound event handler:

<asp:GridView id="GridView1"
OnRowDataBound="GridView1_RowDataBound" ...
<Columns>
<asp:templateField>
<itemtemplate><sp:CheckBox id="CheckBox1" runat="server"></
itemtemplate>
</asp:templateField>

protected void GridView1_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox CheckBox1 = (CheckBox)e.Row.FindControl("CheckBox1");
CheckBox1.Attributes["onclick"] = string.Format("alert({0});",
e.Row.RowIndex);
}
}

Regards,
Mykola
http://marss.co.ua
 
J

Jayakrishnan

I have a few questions regarding accessing a gridview control from
Javascript which I would really appreciate some help with.

1. The gridview contains a column of checkboxes as a template field. I
have onclick Javascript on the checkbox but can't figure out how to
determine the row number of the checkbox that was clicked.

2. How do I access a control within the gridview's pagertemplate? The
pagertemplate contains a 1 row table with a button in it that I need
to enable/disable.

Thanks.


This is for your 2nd?

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewRowEventArgs) Handles
GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.Pager Then
CType(e.Row.FindControl("Button1"), Button).Enabled = False
End If
End Sub

Jayakrishnan
 
M

marksommerville

I have a few questions regarding accessing a gridview control from
Javascript which I would really appreciate some help with.
1. The gridview contains a column of checkboxes as a template field. I
have onclick Javascript on the checkbox but can't figure out how to
determine the row number of the checkbox that was clicked.
2. How do I access a control within the gridview's pagertemplate? The
pagertemplate contains a 1 row table with a button in it that I need
to enable/disable.

Regarding for your first question: you can attach client-side script
within RowDataBound event handler:

<asp:GridView id="GridView1"
OnRowDataBound="GridView1_RowDataBound" ...
<Columns>
<asp:templateField>
<itemtemplate><sp:CheckBox id="CheckBox1" runat="server"></
itemtemplate>
</asp:templateField>

protected void GridView1_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox CheckBox1 = (CheckBox)e.Row.FindControl("CheckBox1");
CheckBox1.Attributes["onclick"] = string.Format("alert({0});",
e.Row.RowIndex);
}

}

Regards,
Mykolahttp://marss.co.ua

Thanks Mykola, I'll give it a shot.
 
M

marksommerville

This is for your 2nd?

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewRowEventArgs) Handles
GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.Pager Then
CType(e.Row.FindControl("Button1"), Button).Enabled = False
End If
End Sub

Jayakrishnan


Thanks but I need to be able to access Button1 from Javascript. The
idea is that if no check boxes are checked then Button1 should be
disabled. My javascript function for the checkbox click will check to
see if all are unchecked then disable the button.
 
J

Jayakrishnan

Thanks but I need to be able to access Button1 from Javascript. The
idea is that if no check boxes are checked then Button1 should be
disabled. My javascript function for the checkbox click will check to
see if all are unchecked then disable the button.

Simple add the html input button in Pager control with name like
"btnPager" and use directly the following code

document.getElementById('btnPager').disabled=true


Jayakrishnan V
Technology Group
 
M

marksommerville

Simple add the html input button in Pager control with name like
"btnPager" and use directly the following code

document.getElementById('btnPager').disabled=true

Jayakrishnan V
Technology Group

I tried using an HTML button and
document.getElementById('btnPager').disabled=true but this only finds
the
first occurence of the button. I have the button within a PageTemplate
and
it displays on the top and bottom pager rows. Anyways, I need to have
an ASP
button so I can run server-side code. In this case, getElementById
doesn't
find the button at all.
Thanks.
 
J

Jayakrishnan

I tried using an HTML button and
document.getElementById('btnPager').disabled=true but this only finds
the
first occurence of the button. I have the button within a PageTemplate
and
it displays on the top and bottom pager rows. Anyways, I need to have
an ASP
button so I can run server-side code. In this case, getElementById
doesn't
find the button at all.
Thanks.

hi,
use a hidden field server control. I hope the following method
definitely helps you.

mark up
<asp:HiddenField ID="HiddenField1" runat="server" />
<asp:GridView ID="GridView1" runat="server" AllowPaging="True">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerTemplate>
<asp:Button ID="Button1" runat="server" Text="Button" />
</PagerTemplate>
<PagerSettings Position="TopAndBottom" />
</asp:GridView>


Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e
As System.Web.UI.WebControls.GridViewRowEventArgs) Handles
GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.Pager Then
If HiddenField1.Value <> Nothing Then
HiddenField1.Value = String.Concat(HiddenField1.Value, ",")
End If
HiddenField1.Value = String.Concat(HiddenField1.Value,
e.Row.FindControl("button1").ClientID)
End If
If e.Row.RowType = DataControlRowType.DataRow Then
Dim chk As CheckBox
chk = e.Row.FindControl("CheckBox1")
chk.Attributes.Add("onclick", "SelectAll(this);")
End If
End Sub


script

var obj = document.getElementById('HiddenField1').value.split(",");
for (j=0;j<obj.length;j++)
{
document.getElementById(obj[j]).disabled=true;
}

Jayakrishnan V
Technology Group
 

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

Top