C# .NET - Gridview Link button - Asked By Jahir on 20-Mar-12 06:28 AM

Earn up to 10 extra points for answering this tough question.
i have register table like this

create table tbl_active(id bigint identity primary key,fullname varchar(100),username varchar(100),password varchar(100),confirmpassword varchar(100),dateofbirth datetime,emailid varchar(100),mobilenumber bigint,address varchar(100), status varchar(100))

insert into tbl_active values('adm','adm','adm','adm','15-Mar-12','mad@gmail.com','456','MI','Active')

insert into tbl_active values('aa','aa','aa','aa','15-Mar-12','mad@gmail.com','456','MI','Inactive')

in gridview if i click edit event  means in status field it want to show link btn active inactive if i click inactve means it want to update inactive in register table and grid view how to do that ?

this is my row bound codings: i put here drop down but i want link btn there ? what i want to change in my codings:


protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)

{

GridView1.EditIndex = e.NewEditIndex;

bindgrid();

}


protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)

{


try


{


if (con.State == ConnectionState.Closed)

{

con.Open();

}


if (e.CommandName == "can")

{

GridView1.EditIndex = -1;

bindgrid();

}


else if (e.CommandName == "upd")

{


LinkButton lkbupd = (LinkButton)e.CommandSource as LinkButton;


GridViewRow row = lkbupd.NamingContainer as GridViewRow;


Label lbid = (Label)row.FindControl("lblid");


TextBox txfullname = (TextBox)row.FindControl("txtfullname");


TextBox txusername = (TextBox)row.FindControl("txtusername");


TextBox txpassword = (TextBox)row.FindControl("txtpassword");


TextBox txconfirmpassword = (TextBox)row.FindControl("txtconfirmpassword");


TextBox txdateofbirth = (TextBox)row.FindControl("txtdateofbirth");


TextBox txemailid = (TextBox)row.FindControl("txtemailid");


TextBox txmobilenumber = (TextBox)row.FindControl("txtmobilenumber");


TextBox txaddress = (TextBox)row.FindControl("txtaddress");


DropDownList txstatus = (DropDownList)row.FindControl("dropdown");



DateTime dateofbirth = new DateTime();

System.Globalization.

CultureInfo Cul = new System.Globalization.CultureInfo("en-GB", true);

dateofbirth =

DateTime.Parse(txdateofbirth.Text.Trim(), Cul, System.Globalization.DateTimeStyles.NoCurrentDateDefault);

cmd =

new SqlCommand("update tbl_active set fullname='" + txfullname.Text + "', username='" + txusername.Text + "',password='" + txpassword.Text + "',confirmpassword='" + txconfirmpassword.Text + "',dateofbirth='" + dateofbirth.ToShortDateString() + "',emailid='" + txemailid.Text + "',mobilenumber='" + txmobilenumber.Text + "',address='" + txaddress.Text + "',status='"+txstatus.SelectedItem.Text+"' where id='" + lbid.Text + "'", con);

cmd.ExecuteNonQuery();

GridView1.EditIndex = -1;

ShowAlertMessage(

"Updated Successfully");

bindgrid();

}


else if (e.CommandName == "del")

{


LinkButton lnkdel = (LinkButton)e.CommandSource as LinkButton;


GridViewRow row = lnkdel.NamingContainer as GridViewRow;


Label lid = (Label)row.FindControl("lblid");

cmd =

new SqlCommand("delete from tbl_active where id='" + lid.Text + "'", con);

cmd.ExecuteNonQuery();

ShowAlertMessage(

"Deleted Successfully");

bindgrid();

}

}


catch (Exception ex)

{

Response.Write(

"Error:" + ex.ToString());

}


finally


{


if (con.State == ConnectionState.Open)

{

con.Close();

}

}

}


this is asp:


<asp:TemplateField HeaderText="ID">


<ItemTemplate>



<asp:Label ID="lblid" runat="server" Text='<%#Eval("id") %>'></asp:Label>



</ItemTemplate>


</asp:TemplateField>



<asp:TemplateField HeaderText="Full Name">



<ItemTemplate>



<asp:Label ID="lblfullname" runat="server" Text='<%#Eval("fullname") %>'></asp:Label>



</ItemTemplate>



<EditItemTemplate>



<asp:TextBox ID="txtfullname" runat="server" Text='<%#Eval("fullname") %>'></asp:TextBox>



</EditItemTemplate>


</asp:TemplateField>



<asp:TemplateField HeaderText="UserName">



<ItemTemplate>



<asp:Label ID="lblusername" runat="server" Text='<%#Eval("username") %>'></asp:Label>



</ItemTemplate>



<EditItemTemplate>



<asp:TextBox ID="txtusername" runat="server" Text='<%#Eval("username") %>'></asp:TextBox>



</EditItemTemplate>


</asp:TemplateField>



<asp:TemplateField HeaderText="Password">



<ItemTemplate>



<asp:Label ID="lblpassword" runat="server" Text='<%#Eval("password") %>'></asp:Label>



</ItemTemplate>



<EditItemTemplate>



<asp:TextBox ID="txtpassword" runat="server" Text='<%#Eval("password") %>'></asp:TextBox>



</EditItemTemplate>


</asp:TemplateField>



<asp:TemplateField HeaderText="Confirm Password">



<ItemTemplate>



<asp:Label ID="lblconfirmpassword" runat="server" Text='<%#Eval("confirmpassword") %>'></asp:Label>



</ItemTemplate>



<EditItemTemplate>



<asp:TextBox ID="txtconfirmpassword" runat="server" Text='<%#Eval("confirmpassword") %>'></asp:TextBox>



<asp:CompareValidator ID="CompareValidator1" runat="server"


ControlToCompare="txtconfirmpassword" ControlToValidate="txtpassword"


ErrorMessage="Password Not Match" ValidationGroup="a"></asp:CompareValidator>



</EditItemTemplate>


</asp:TemplateField>



<asp:TemplateField HeaderText="DateOfBirth">



<ItemTemplate>



<asp:Label ID="lbldateofbirth" runat="server" Text='<%#Eval("dateofbirth") %>'></asp:Label>



</ItemTemplate>



<EditItemTemplate>



<asp:TextBox ID="txtdateofbirth" runat="server" Text='<%#Eval("dateofbirth") %>'></asp:TextBox>



<asp:CalendarExtender ID="Cal2" runat="server" Format="dd/MM/yyyy" TargetControlID="txtdateofbirth"> </asp:CalendarExtender>



</EditItemTemplate>


</asp:TemplateField>



<asp:TemplateField HeaderText="Mail Id">



<ItemTemplate>



<asp:Label ID="lblemailid" runat="server" Text='<%#Eval("emailid") %>'></asp:Label>



</ItemTemplate>



<EditItemTemplate>



<asp:TextBox ID="txtemailid" runat="server" Text='<%#Eval("emailid") %>'></asp:TextBox>



</EditItemTemplate>


</asp:TemplateField>



<asp:TemplateField HeaderText="Mobile Number">



<ItemTemplate>



<asp:Label ID="lblmobilenumber" runat="server" Text='<%#Eval("mobilenumber") %>'></asp:Label>



</ItemTemplate>



<EditItemTemplate>



<asp:TextBox ID="txtmobilenumber" runat="server" Text='<%#Eval("mobilenumber") %>'></asp:TextBox>



</EditItemTemplate>


</asp:TemplateField>



<asp:TemplateField HeaderText="Address">



<ItemTemplate>



<asp:Label ID="lbladdress" runat="server" Text='<%#Eval("address") %>'></asp:Label>



</ItemTemplate>



<EditItemTemplate>



<asp:TextBox ID="txtaddress" runat="server" Text='<%#Eval("address") %>'></asp:TextBox>



</EditItemTemplate>


</asp:TemplateField>



<asp:TemplateField HeaderText="Status" >



<ItemTemplate>



<asp:Label ID="lblstatus" runat="server" Text='<%#Eval("status") %>'></asp:Label>



</ItemTemplate>



<EditItemTemplate>



<asp:DropDownList ID="dropdown" runat="server">



<asp:ListItem>Active</asp:ListItem>



<asp:ListItem>Inactive</asp:ListItem>



</asp:DropDownList>



</EditItemTemplate>


</asp:TemplateField>



<asp:TemplateField HeaderText="EDIT/UPDATE">



<ItemTemplate>



<asp:LinkButton ID="btnedit" runat="server" CommandName="Edit" Text="Edit">



</asp:LinkButton>



</ItemTemplate>



<EditItemTemplate>



<asp:LinkButton ID="btnupdate" runat="server" Text="Update" OnClientClick="return confirm('Do You Want To Update')" CommandName="upd"></asp:LinkButton>



<asp:LinkButton ID="btncancel" runat="server" CommandName="can" Text="Cancel"></asp:LinkButton>



</EditItemTemplate>



</asp:TemplateField>



<asp:TemplateField HeaderText="DELETE">



<ItemTemplate>



<asp:LinkButton ID="btndelete" runat="server" Text="Delete" OnClientClick="return confirm('Do You Want to Delete')" CommandName="del"></asp:LinkButton>



</ItemTemplate>



</asp:TemplateField>



D Company replied to Jahir on 20-Mar-12 01:14 PM
There are two way of doing this either u can write a javascript functin onclick  event of the link button. pass the id of the control to the javascript function and check the value if it is inactive then make it active or vice-verse or the second option is u can write the code on rowdatabound event of grid as follows

protected void yourGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
//check if it is a linkbutton
LinkButton button = e.Row.FindControl("yourlinkbuttonname") as button;
if(button != null)
{
button.Click +=new EventHandler("yourlinkbuttonname_Click");
}
}
}
protected void yourlinkbuttonname_Click(object sender, EventArgs e)
{
LinkButton button = (LinkButton)sender;
if (button .Text == "Active")
{
button .Text="Inactive";
}
else
{
button .Text =="Active";

Hope this helps
Regards
D
[)ia6l0 iii replied to Jahir on 20-Mar-12 09:10 PM
I don't understand the "register" table. Your first SQL Script seems to be talking about "tbl_active" and not "register". You need to be clear. 

I guess you are using the "Status" field to mark a user active/inactive. Regardless of what actions you write in whatever event of ghe GridView control, you should first change this to an Integer instead of varchar(100). Use Enums on your C# code to return you the integer values. You can define an Enum that contains the Active Status like, 

public Enum Status
{
InActive=0,
Active = 1
}

Coming back to your original requirement of adding link buttons to the gridview, you can do as follows:

<asp:TemplateField>
<HeaderTemplate>
Linkbutton header
</HeaderTemplate>
<ItemTemplate>
      <asp:LinkButton id="lnkbtn" runat="server" CommandName="editlink" CommandArgument='<%#Eval("status") %>' ="Edit"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>

And if you want to just toggle visibility, use the GridViewEdit command. Sample code below.
protected void GridViewEdit(object sender, GridViewEditEventArgs e)
{
    GridViewRow row = employeesGridView.Rows[e.NewEditIndex];
    //Toggle the visibility.
    (employeesGridView.Rows[row.RowIndex].FindControl("lnkbtn")).Visible = true;      
}

Hope this helps.
kalpana aparnathi replied to Jahir on 21-Mar-12 04:42 AM
hi,

Try with this:

LinkButton button = (LinkButton)e.Row.FindControl("txtstatus");

Regards,