Showing posts with label drop down list. Show all posts
Showing posts with label drop down list. Show all posts

Friday, September 19, 2014

Get HTML Select(Drop Down List) value in Code Behind (C#) in ASP.NET

Sometimes its very mush important to get the value of the HTML Drop Down List, Select value from the code behind (C#). So how to get the value of a HTML control value in the code behind. A very easy thing to do.

Create a new project and add a WebForm and add a new select tag with some value. Like this



Now in the C# page write the code to get the value.
string ddl = "";
// to get the selected value
ddl = Requets.Form["ddl"].ToString();

To get the selected value the actual code is
Requets.Form["ddl"]

In this way you can get the data of a HTML field from code behind (C#). With in that click event write down the following code

Monday, July 21, 2014

Bind two data columns as DataTextField in a DropDownList with SQLDataSource in ASP.NET C#

Hello! To day i will show you how to bind two data fields into one DataTextField or DataValueField in a DropDownList in ASP.NET.

Table structure :

Id             Name           
1        Arkadeep De
2        Pradeep Das
3        Praphul Kumar

Now in your DropDownList your data will show llike

Arkadeep De (1)
Pradeep Das (2)
Praphul Kumar (3)

And in your value will be as your data in Id column ( 1,2,3...).

So how to do that with SQLDataSource !

First create a new project and add a new web form. After that add a new DropDownList and a New SQLDataSource. Bind the DropDownList with the SQLDataSource.

<asp:DropDownList ID="ddlTID_NO" runat="server" DataSourceID="SqlDataSource1"> </asp:DropDownList>

Now in the SelectCommand of the SQLDataSource write down the following query.

SELECT [ID], [NAME]+'  ('+Id+') ' as Name1 FROM [TABLE_NAME] order by ID

Now in the DropDownList add the DataTextField=Name1 and DataValueField=Id

Your project is ready. Now run the project and check the result.

Saturday, July 12, 2014

Add row after in DropDownList binding with SQLDataSource in ASP.NET C#

After binding a DropDownList  in C# using SQLDataSource its showing data all time what ever its bind from the database's table. But if you an one need to add any additional data into the DropDownList  then how to do.

In this article I will show you how to add a row  in a DropDownList  after binding with a SQLDataSource.

First bind a DropDownList with a SQLDataSource and the in the page_Load event write down the following code.

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
         ddl1.Items.Insert(0, new ListItem("-Select-", "0"));
         ddl1.DataBind();
    }
}

Now run the project and see the change. Enjoy... :)

Tuesday, May 13, 2014

Get HTML Drop Down List value in code behind C#

Few days ago I stuck into a problem , I need to get the value of from a HTML drop down list in my c# code behind page. So I started searching and found the solution.  Very simple solution indeed. Just a single line code to end the problem.

Suppose the drop down list code is


<select name="cars">
  <option value="volvo">Volvo XC90</option>
  <option value="saab">Saab 95</option>
  <option value="mercedes">Mercedes SLK</option>
  <option value="audi">Audi TT</option>
</select>

And a ASP button to get the value..


<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />

Now on the C# page write the bellow code.


var car = Request["cars"];
Response.Write(car.ToString());

If you are using ASP Drop Down List then the code will be little bit different.


<asp:DropDownList ID="DropDownList1" runat="server">
   <asp:ListItem>Item1</asp:ListItem>
   <asp:ListItem>Item2</asp:ListItem>
   <asp:ListItem>Item3</asp:ListItem>
</asp:DropDownList>

And on button click..


Response.Write(DropDownList1.SelectedValue.ToString());
Response.Write(DropDownList1.SelectedItem.Text.ToString());

Now enjoy the code..

Popular Posts

Pageviews