Wednesday 1 October 2014

Access html tag in c# code behind and Set value to html tag from c# code behind

Accessing html tag data or value from c# code behind

Here is my Asp.Net page 


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestingHtmlControl.aspx.cs" Inherits="WebApplication2.TestingHtmlControl" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <select name="sltTest" id="slt">
        <option value="test1">test1</option>
        <option value="test2">test2</option>
        <option value="test3">test3</option>
        <option value="test4">test4</option>
    </select>
    </div>
        <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
    </form>
</body>
</html>

Now I am going to access my html select tag selected value from C#.Net code behind  

C#.Net Code

//Submit button click event
 protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string seletedDropDownValueOfHtmlSelect = "";
            seletedDropDownValueOfHtmlSelect = Request.Form["sltTest"].ToString();

//Assigning value to html select tag from C#.Net code behind
            var script = "document.getElementById('slt').value = 'test3';";
            ClientScript.RegisterStartupScript(typeof(string), "textvaluesetter", script, true);

            Response.Write("Name : " + seletedDropDownValueOfHtmlSelect);

        }

No comments:

Post a Comment