Coding Cluster - using asp.net, c#, mvc 4, iphone, php, ios, javascript, in asp.net mvc 3 & more
 

Source code: Create Dynamic LinkButton and Click event for dynamic linkbuttons in asp.net

Wednesday

ASP.NET Dynamic LinkButton  using c#(Create dynamic html table in asp.net):
                                          In this article I am explaining how to add Dynamic table with Dynamic LinkButton. In the design page, we just add how many controls we need and where it should be placed. These controls are static and having the data what we gave. In some cases we have to dynamically get data from the database and create dynamic controls for the data.

      Creating Dynamic LinkButton in Asp.Net is simple one . After creating the dynamic controls just add the controls in a PlaceHolder or Panel control.

       In the following sample, I'm created a html table with students name  at run time. Here i'm created dynamic link for all students at the time of binding. The dynamic click event "lnkStudentDetails_Click" redirect to corresponding student details page.

       In the Page_Load event, Call the load data method outside the IsPostBack() method. Because our dynamic controls should recreate each and every time when a postback call.

DynamicLinkButton.aspx
"C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Coding Cluster - Dynamic Link Button</title>
</head>
<body>
<form runat="server" id="f1">
    <asp:Panel ID="pnlStudentDynamic" runat="server">
    
    </asp:Panel>
    </form>
</body>
</html>

Sample:


                       

DynamicLinkButton.aspx.cs
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    // Create object for your html table
    HtmlTable tblStudents = new HtmlTable();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //Do nothing
        }
       
       //Load students name
        LoadStudents();
    }

    public void LoadStudents()
    {
       // Connection string for your database
        SqlConnection SqlConn = new SqlConnection("Data Source=localhost;Initial Catalog=Student;User ID=sa;Password=sa");
        SqlDataAdapter SqlDap = new SqlDataAdapter("select * from StudentMaster", SqlConn);
        DataTable objDT = new DataTable();
        SqlDap.Fill(objDT);

        tblStudents.ID = "tblStudent";
        tblStudents.Width = "40%";
        tblStudents.CellPadding = 5;
        int intRowCount = objDT.Rows.Count;

        if ((intRowCount > 0))
        {
            int sCount = (intRowCount / 2) + 1;
            int intCurrentCount = 0;
            for (int i = 1; i <= sCount; i++)
            {
                HtmlTableRow tblRowDef = new HtmlTableRow(); tblRowDef.Align = "center"; if ((intRowCount > intCurrentCount))
                {
                    for (int l = 1; l <= 2; l++)
                    {
                        if ((intRowCount > intCurrentCount))
                        {
                            HtmlTableCell tblCellDef = new HtmlTableCell();
                            tblCellDef.ID = "0" + objDT.Rows[intCurrentCount]["Student_Name"].ToString();
                            tblCellDef.Width = "50px";
                            LinkButton lnkStudent = new LinkButton();
                            lnkStudent.ID = "lnkStudent" + objDT.Rows[intCurrentCount]["Student_Name"].ToString();//You can change with Student_ID
                            lnkStudent.Text = objDT.Rows[intCurrentCount]["Student_Name"].ToString();
                            lnkStudent.CommandName = objDT.Rows[intCurrentCount]["Student_ID"].ToString();
                            lnkStudent.Font.Bold = true;
                            lnkStudent.Font.Size = 13;
                            lnkStudent.Width = 10;
                            //Create dynamic click event for all link buttons
                            lnkStudent.Click += new EventHandler(lnkStudentDetails_Click);
                            tblCellDef.Controls.Add(lnkStudent);
tblRowDef.Controls.Add(tblCellDef); intCurrentCount += 1; } } } tblStudents.Controls.Add(tblRowDef); } } pnlStudentDynamic.Controls.Add(tblStudents); } protected void lnkStudentDetails_Click(object sender, EventArgs e) { LinkButton lnkStudent = (LinkButton)sender;
        //Redirect to student details page
        Response.Redirect("StudentDetails.aspx?ID=" + lnkStudent.CommandName);
    }

}
If this post was help to you. Then Share this to your friends. Thanks!

Solution:The type or namespace name 'SqlConnection' could not be found (are you missing a using directive or an assembly reference?)

How to fix - The type or namespace name 'SqlConnection' could not be found - error in asp.net?:
                                                         "The type or namespace name ' SqlConnection ' could not be found (are you missing a using directive or an assembly reference?)" error means you just missed to add appropriate namespace for connect your sql database. To fix this issue you just add  the following  namespace in your page top.

            using System.Data.SqlClient;

ASP.NET: How to Show/ Hide Div Tags based on radio button selection

Show/hide div based on radio button selection in asp.net:
                                                                       This post will show you how to create a hidden Div and display it with the click of a link. The following code will Show / Hide a div tags  when changing the selection of a radio button.

Sample code for show/hide div tag from code-behind using asp.net:

@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Hide div</title>

    <script runat="server">
        protected void rbtnHideDiv_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            if (rbtnHideDiv.SelectedItem.Text == "Red")
            {
                divRed.Style.Add("display", "none");
                divGreen.Style.Add("display", "inline");
            }
            if (rbtnHideDiv.SelectedItem.Text == "Green")
            {
                divGreen.Style.Add("display", "none");
                divRed.Style.Add("display", "inline");
            }
        }
    </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:RadioButtonList ID="rbtnHideDiv" runat="server" AutoPostBack="true" OnSelectedIndexChanged="rbtnHideDiv_SelectedIndexChanged">
            <asp:ListItem>Red</asp:ListItem>
            <asp:ListItem>Green</asp:ListItem>
        </asp:RadioButtonList>
    </div>
    <div style="background-color: Red" id="divRed" runat="server">
        <asp:Label ID="lblTextRed" SkinID="label_information" Text="My background is RED"
            ForeColor="White" runat="server"></asp:Label>
    </div>
    <div style="background-color: Green" id="divGreen" runat="server">
        <asp:Label ID="lblGreen" SkinID="label_information" Text="My background is GREEN"
            ForeColor="White" runat="server"></asp:Label>
    </div>
    </form>
</body>
</html>

                                   

Sample code for show/hide div tag using java script in asp.net:

<html>
<head runat="server">
    <title></title>
     <script type="text/javascript" language="javascript">

         function hideDiv() {
             document.getElementById('div1').style.display = 'none';
             document.getElementById('div2').style.display = 'none';

             if (document.getElementById('rbtnMain_0') != null) {
                 if (document.getElementById('rbtnMain_0').checked) {
                     document.getElementById('div1').style.display = 'block';
                 }
             }

             if (document.getElementById('rbtnMain_1') != null) {
                 if (document.getElementById('rbtnMain_1').checked) {

                     document.getElementById('div2').style.display = 'block';

                 }
             }
         }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:RadioButtonList ID="rbtnMain" runat="server" onchange="hideDiv()">
            <asp:ListItem Text="rb1" Value="1" Selected="True"></asp:ListItem>
            <asp:ListItem Text="rb2" Value="2"></asp:ListItem>
        </asp:RadioButtonList>
        <div id="divmain">
        <div id="div1">
           div1 <asp:Button ID="Button1" runat="server" Text="Button" />
        </div>
        <div id="div2" style="display:none">
           div2  <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        </div>
        </div>
        
    </div>
    </form>
</body>
</html>
If this post was help to you. Then Share this to your friends. Thanks!

 
 
 

RECENT POSTS

Boost

 
Blogger Widgets