javascript on link button which is in gridview

Posted by Lalitha86 under DotNetFunda.Com on 8/14/2012 | Points: 10 | Views : 7348 | Status : [Member] | Replies : 5
i have a link button which is inside the grid
i am calling a javascript on the link button but geeting an error as



    protected void gvReport_RowDataBound(object sender, GridViewRowEventArgs e)
{


if (e.Row.RowType == DataControlRowType.DataRow)
{

//identifying the control in gridview
LinkButton lnkbtnresult = (LinkButton)e.Row.FindControl("lnkDate");
//raising javascript confirmationbox whenver user clicks on link button
lnkbtnresult.Attributes.Add("onClick", "javascript:setFunc()");



}



if (e.Row.RowType == DataControlRowType.Header)
{
GridView HeaderGrid = (GridView)sender;

GridViewRow HeaderGridRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
TableCell HeaderCell = new TableCell();
HeaderCell.Text = "<b></b>";
HeaderCell.ColumnSpan = 1;
HeaderCell.BorderColor = System.Drawing.Color.Black;
HeaderGridRow.Cells.Add(HeaderCell);
TableCell HeaderCell1 = new TableCell();
HeaderCell1.Text = "<b>Capital Listed</b>";
HeaderCell1.ColumnSpan = 4;
HeaderCell1.BorderColor = System.Drawing.Color.Black;
HeaderGridRow.Cells.Add(HeaderCell1);



gvReport.Controls[0].Controls.AddAt(0, HeaderGridRow);


}
}



and my
function is

function setFunc() {
var lbl1 = document.getElementById("ctl00_ContentPlaceHolder1_lbl_year");
lbl1.focus();
alert("Hello ");
}


and aspx page
<asp:GridView ID="gvReport" width="100%"
runat="server" CellPadding="4" CellSpacing="1" GridLines="None"
AutoGenerateColumns="False" OnRowCommand="gvReport_RowCommand" CssClass="mGrid" PagerStyle-CssClass="pgr" OnRowDataBound="gvReport_RowDataBound" >

<HeaderStyle CssClass="TTHeader"/>
<Columns>


<asp:TemplateField HeaderText="Year" ItemStyle-CssClass="TTRow" ControlStyle-CssClass="tablebluelink" ControlStyle-Width="55px">
<ItemTemplate >
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("YR") %>'/>
<asp:LinkButton ID="Linkbtn" runat="server" Text='<%# Eval("Year") %>' CommandName="lnkDate" ></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>




Responses

Posted by: Ranjeet_8 on: 8/14/2012 [Member] [MVP] Gold | Points: 25

Up
0
Down
Try this
  

<asp:GridView ID="gvReport" Width="100%" runat="server" CellPadding="4" CellSpacing="1"
GridLines="None" AutoGenerateColumns="False" OnRowCommand="gvReport_RowCommand"
CssClass="mGrid" PagerStyle-CssClass="pgr" OnRowDataBound="gvReport_RowDataBound">
<HeaderStyle CssClass="TTHeader" />
<Columns>
<asp:TemplateField HeaderText="Year" ItemStyle-CssClass="TTRow"
ControlStyle-CssClass="tablebluelink" ControlStyle-Width="55px">
<ItemTemplate>
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("YR") %>' />
<asp:LinkButton ID="Linkbtn" runat="server" Text='<%# Eval("Year") %>'
CommandName="lnkDate" OnClientClick="setFunc();"></asp:LinkButton>
<script language="javascript" type="text/javascript">
function setFunc() {
var lbl1 = document.getElementById("ctl00_ContentPlaceHolder1_lbl_year");
lbl1.focus();
alert("Hello ");
}
</script>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>


Lalitha86, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Lalitha86 on: 8/14/2012 [Member] Starter | Points: 25

Up
0
Down
What u mean to say
Please explain

Lalitha86, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ranjeet_8 on: 8/14/2012 [Member] [MVP] Gold | Points: 25

Up
0
Down
Add your Java script code inside the GridView.

Lalitha86, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Lalitha86 on: 8/14/2012 [Member] Starter | Points: 25

Up
0
Down
but may i know Why
to do this way reason for this

Lalitha86, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Lalitha86 on: 8/14/2012 [Member] Starter | Points: 25

Up
0
Down
i am geting the hello alert work but focus is not working

Lalitha86, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response