Click here to Skip to main content
15,937,602 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Experts,

I am required to add Template Field which contains LinkButton in GridView at Run time.
And OnClick of link button I want to perform some operation.
Is it Possible??

and if it's possible give me solution.

Thanking You,
Prashant

:thumbsup:
Posted
Updated 25-Jul-10 19:04pm
v2

VB
Private Sub ExportData()
        'Try
        objAttendance.Mode = "SelectEmpDetailsWithDeptAndDesignation"
        objAttendance.CmdType = "Select"
        objAttendance.CompanyID = Session("g_Company").ToString()
        objAttendance.EmpCode = IIf(ddlEmployee.SelectedItem.Text = "All", 0, ddlEmployee.SelectedItem.Value)
        objAttendance.DeptID = IIf(ddlDepartment.SelectedItem.Text = "All", 0, ddlDepartment.SelectedItem.Value)
        Dim ds As New DataSet
        ds = clsAttendance.SelectData(objAttendance)
        Dim sel_Month As Integer
        Dim sel_Year As Integer
        Dim sel_Date As Integer
        If IsDate(txtDate.Text) Then
            sel_Date = Convert.ToDateTime(txtDate.Text).Day
            sel_Month = Convert.ToDateTime(txtDate.Text).Month
            sel_Year = Convert.ToDateTime(txtDate.Text).Year
        Else
            sel_Month = Convert.ToInt32(ddlMonth.SelectedValue.Trim)
            sel_Year = Convert.ToInt32(ddlYear.SelectedValue.Trim)
        End If

        Dim dtReport As New DataTable
        dtReport.Columns.Add("Employee Code")
        dtReport.Columns.Add("Employee Name")
        dtReport.Columns.Add("Department")
        dtReport.Columns.Add("Designation")
        dtReport.Columns.Add("Date")
        dtReport.Columns.Add("Day")
        dtReport.Columns.Add("Lon-In Time")
        dtReport.Columns.Add("Log-Out Time")
        dtReport.Columns.Add("Total Hours")

        Dim ds_details As New DataSet
        Dim str_time As String = ""
        Dim dtCheckIn As DateTime = DateTime.Now
        Dim dtCheckOut As DateTime = DateTime.Now
        Dim j As Integer
        Dim i As Integer
        Dim k As Integer = 0
        For i = 0 To ds.Tables(0).Rows.Count - 1
            ds_details = clsAttendance.SelectUserTimeLog(ds.Tables(0).Rows(i).Item("V_Emp_Code"), sel_Month, sel_Year, sel_Date)
            For j = 0 To ds_details.Tables(0).Rows.Count - 1
                dtReport.Rows.Add()
                If j = 0 Then
                    dtReport.Rows(k)("Employee Code") = ds.Tables(0).Rows(i)("V_Emp_Code")
                    dtReport.Rows(k)("Employee Name") = ds.Tables(0).Rows(i)("V_Emp_Name")
                    dtReport.Rows(k)("Department") = ds.Tables(0).Rows(i)("V_Dept_Name")
                    dtReport.Rows(k)("Designation") = ds.Tables(0).Rows(i)("V_Designation_Name")
                End If
                dtReport.Rows(k)("Date") = Format$(Convert.ToDateTime(ds_details.Tables(0).Rows(j)("EmpAttDetDate")), "dd-MMM-yyyy")
                dtReport.Rows(k)("Day") = Format$(Convert.ToDateTime(ds_details.Tables(0).Rows(j)("EmpAttDetDate")), "dddd")
                Dim time_checkin As String = ""
                ''for CheckInTime
                If (ds_details.Tables(0).Rows(j)("CheckInTime").ToString() <> "" And ds_details.Tables(0).Rows(j)("CheckInTime").ToString() <> "&nbsp;") Then
                    time_checkin = ds_details.Tables(0).Rows(j)("CheckInTime")
                    time_checkin = time_checkin.Substring(0, time_checkin.IndexOf(":") + 3)
                    dtCheckIn = Convert.ToDateTime("01-01-2008 " + time_checkin)
                    time_checkin = dtCheckIn.ToString("hh:mm tt")
                Else
                    time_checkin = ""
                End If
                Dim time_checkout As String = ""
                ''for CheckOutTime
                If (ds_details.Tables(0).Rows(j)("CheckOutTime").ToString() <> "" And ds_details.Tables(0).Rows(j)("CheckOutTime").ToString() <> "&nbsp;") Then
                    time_checkout = ds_details.Tables(0).Rows(j)("CheckOutTime")
                    time_checkout = time_checkout.Substring(0, time_checkout.IndexOf(":") + 3)
                    dtCheckOut = Convert.ToDateTime("01-01-2008 " + time_checkout)
                    time_checkout = dtCheckOut.ToString("hh:mm tt")
                Else
                    time_checkout = ""
                End If
                ''for Total Hours
                If (ds_details.Tables(0).Rows(j)("TotalHour").ToString() <> "" And ds_details.Tables(0).Rows(j)("TotalHour").ToString() <> "&nbsp;") Then
                    str_time = ds_details.Tables(0).Rows(j)("TotalHour")
                    str_time = Format(str_time, "Short Time")
                Else
                    str_time = ""
                End If
                dtReport.Rows(k)("Lon-In Time") = time_checkin
                dtReport.Rows(k)("Log-Out Time") = time_checkout
                dtReport.Rows(k)("Total Hours") = str_time
                k = k + 1
            Next
        Next
        Dim GV As New GridView
        GV.DataSource = dtReport
        GV.DataBind()
        Response.Clear()
        Response.Buffer = True
        Response.AddHeader("content-disposition", "attachment;filename=EmployeeAttendance.xls")
        Response.Charset = ""
        Response.ContentType = "application/vnd.ms-excel"
        Dim sw As New StringWriter()
        Dim hw As New HtmlTextWriter(sw)
        For l As Integer = 0 To GV.Rows.Count - 1
            'Apply text style to each Row
            GV.Rows(l).Attributes.Add("class", "textmode")
        Next
        GV.RenderControl(hw)
        Dim style As String = "<style> .textmode { mso-number-format:\@; } </style>"
        ''Response.Write(sw.ToString)
        Response.Output.Write(sw.ToString())
        Response.Flush()
        Response.End()

        'Catch ex As Exception
        '    showlabelmessage("Sorry Some Server Error has occured..Please Contact Your Administrator", lblError)
        '    RegisterBug(Request.Url.ToString, ex.Message, Err.Number, New System.Diagnostics.StackTrace().GetFrame(0).GetMethod().Name, System.Net.Dns.GetHostAddresses(My.Computer.Name)(0).ToString, Session("g_User"), Me.Title)
        'End Try
    End Sub
 
Share this answer
 
hi
yeah, it is possible.

In Html page:
XML
<asp:GridView runat=server ID=GridView1  CellPadding=3 CellSpacing=0 AutoGenerateColumns=false >   <asp:TemplateField HeaderText="Column1">
     <ItemTemplate>
     <asp:LinkButton runat=server ID=LinkButton1  Text='<%#Bind("Column1") %>'  onClick="LinkButton1_Click"/>
     </ItemTemplate>
   </asp:TemplateField>
     <asp:BoundField DataField="Column2" HeaderText="Column2" />
     <asp:BoundField DataField="Column3" HeaderText="Column3" />
  </Columns>
  </asp:GridView>


In .aspx.cs code:
private void BindGridView()
{
DataTable datatable = new DataTable();
datatable.Columns.Add("Column1", typeof(System.String));
datatable.Columns.Add("Column2", typeof(System.String));
datatable.Columns.Add("Column3", typeof(System.String));
datatable.Rows.Add("http://123.com", "2", "3");
datatable.Rows.Add("http://abc.com", "5", "6");
datatable.Rows.Add("http://bcd.com", "7", "8");
this.GridView1.DataSource = datatable;
this.GridView1.DataBind();
}

C#
protected void LinkButton1_Click(object sender, EventArgs e)
       {
           //you can get selected rowindex
           int rowIndex = (((sender as LinkButton).NamingContainer) as GridViewRow).RowIndex;
           GridViewRow GridViewRow1 = this.GridView4.Rows[rowIndex];
......

       }


good lucky to u.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900