Selasa, 24 September 2013

How to access Html select tag in code file

HtmlSelect Class

The HtmlSelect class creates an HTML select control, which can display either a list box or a drop-down list box depending on its attribute. The items of the select control are stored in <option>elements inside the <select> element. As soon as you add a list box or drop-down list box to a Web form using HTML server controls, ASP.NET adds a default , empty <option> element to the <select> element. The HtmlSelect class gives you access to the HTML <select> element s in server code.

Properties of the HtmlSelect Class

DataMember  : Obtains or sets the set of data to bind to the HtmlSelect control from a DataSource with multiple sets of Data.
DataSource : Obtains or sets the source of information to bind to the HtmlSelect control.
DataSourceId : Obtains or sets the ID of the Data source control that the HtmlSelect control should use to retrieve its data source.
DataTextField : Obtains or sets the field from the data source to bind to the System.Web.UI.WebControls.ListItem.Text property of each item in the HtmlSelect control.
DataValueField : Obtains or sets the field from the data source to bind to the System.Web.UI.WebControls.ListItem.value property of each item in the HtmlSelect control.
InnerHtml : Obtains or sets the conect between the opening and closing tags of the control without automatically converting special characters to their equivalent HTMl entities.
Items : Obtains a collection that contains the item listed in an HtmlSelect control.
Multiple : Obtains or sets a value indicating whether multiple items can be selected concurrently in the HtmlSelect control.
Name : Obtains or sets the unique identifier name associated with the HtmlSelect control.
SelectedIndex : Obtains or sets the ordinal index of the selected item in an HtmlSelect control.
Size : Obtains or sets the height (in row ) of the HtmlSelect control.
Value : Obtains the value of the selected item in the HtmlSelect control or sets the SelectedIndex property of the control to the index of the first item in the list with the specified value.

Event of the HtmlSelect Class


ServerChange : Occurs when the select control is changed. You can handle this event on the server.

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    protected void click_server(object server, EventArgs e)
    {
        sp1.InnerHtml = Select1.Value.ToString();
        int i = 0;
        for(i=0;i<Select1 .Items.Count;i++)
        {
            if (Select1 .Items [i].Selected)
            {
                sp2.InnerHtml += Select1.Value.ToString();
            }
        }
    }

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    Please Select Item : <br />
        <select id="Select1" runat ="server" >
            <option value ="Apple" selected ="selected">Apple</option>
            <option value ="Mango">Mango</option>
                   <option value ="Orange">Orange</option>
        </select>
        <span runat ="server" id="sp1">
        </span>
        <span runat ="server" id="sp2">
        </span>
        <input id="Submit1" type="submit" value="submit" runat ="server" onserverclick ="click_server" />
    </div>
    </form>
</body>
</html>

Output
How to access Html select tag in code file

Tidak ada komentar:

Posting Komentar