Sponsored Ad

Wednesday, April 14, 2010

How to Fill HTML Dropdown(SELECT) From C# | Call C# Method From HTML Code

This sample code will help you to call an C# method from HTML code using server tags. there is two ways to fill HTML select dropdown.

1. Hardcoded values

2. Iterating through dataset

 

How to Fill HTML Dropdown(SELECT) From C# | Call C# Method From HTML Code

 

ASP.NET code:

<html  >
<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript">
    </script>
</head>
<body>
    <form id="form1" runat="server">
    Method 1:
    <select id="tmplPriority" name="tmplPriority1" >
                        <%=LoadList1()%>
                    </select>
                    <br />
                    <br />
       Method 2:
    <select id="Select1" name="tmplPriority2" >
                        <%=LoadList2()%>
                    </select>
    </form>
</body>
</html>

 

C# code:

use namespace

using System.Data;

 

protected void Page_Load(object sender, EventArgs e)
    {
        LoadList1();
        LoadList2();
    }
    public string LoadList1()
    {

        string sOption = "";
        sOption = sOption + "<OPTION VALUE=" + "Option 1" + ">" + "Option 1" + "<//OPTION>";
        sOption = sOption + "<OPTION VALUE=" + "Option 2" + ">" + "Option 2" + "<//OPTION>";
        sOption = sOption + "<OPTION VALUE=" + "Option 3" + ">" + "Option 3" + "<//OPTION>";
        sOption = sOption + "<OPTION VALUE=" + "Option 4" + ">" + "Option 4" + "<//OPTION>";
        return sOption;
    }

    public string LoadList2()
    {
        //Fill using dataset
        string sOption = "";
        DataSet oDs = Get_Data();
        if (oDs != null && oDs.Tables.Count > 0)
        {
            for (int irow = 0; irow < oDs.Tables[0].Rows.Count; irow++)
                sOption = sOption + "<OPTION VALUE=" + oDs.Tables[0].Rows[irow][0] + ">" + oDs.Tables[0].Rows[irow][1] + "<//OPTION>";

            return sOption;
        }
        else
            return "<OPTION></OPTION>";
    }

    private DataSet Get_Data()
    {
        DataSet ds = new DataSet();
          DataTable dt = new DataTable();
          try
          {
              dt.Columns.Add(new DataColumn("value",Type.GetType("System.String")));               
              dt.Columns.Add(new DataColumn("text",Type.GetType("System.String")));
              dt.AcceptChanges();
              DataRow dr = dt.NewRow();
              //First Row
              dr["value"] = "Option 1";
              dr["text"] = "Option 1";
              dt.Rows.Add(dr);
              dt.AcceptChanges();
              //Second Row
              dr=null;
              dr=dt.NewRow();
              //First Row
              dr["value"] = "Option 2";
              dr["text"] = "Option 2";
              dt.Rows.Add(dr);
              dt.AcceptChanges();
              //Third Row
              dr=null;
              dr=dt.NewRow();
              //First Row
              dr["value"] = "Option 3";
              dr["text"] = "Option 3";
              dt.Rows.Add(dr);
              dt.AcceptChanges();

              ds.Tables.Add(dt);

              return ds;
          }
          catch(Exception ex)
          {
              throw;
          }
          finally
          {
              if(dt != null)
              {
                  dt =null;
              }
          }
      }

0 comments:

Post a Comment

Sponsored Ad

More Related Articles

Website Update

Followers