show label data based on gridview data

RAVI 916 Reputation points
2024-04-04T05:37:32.4866667+00:00

Hello

I have girdivew1 in that i have this data forexample

User's image

And i have one dropdownlist and one label

if i select from dropdownlist MANGO it has to in label as 10

if i select from dropdownlist APPLE it has to in label as 4

how to do so

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,288 questions
0 comments No comments
{count} votes

Accepted answer
  1. Lan Huang-MSFT 25,876 Reputation points Microsoft Vendor
    2024-04-04T07:32:44.8933333+00:00

    Hi @RAVI,

    Is your girdivew data obtained from the database? If so, you can refer to the code below.

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
        <script>
            $(function () {
                $("#DropDownList1").change(function () {
                    document.getElementById('Label1').innerHTML = this.value;
                })
            })
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
                <asp:Label ID="Label1" runat="server"></asp:Label>
                <asp:GridView ID="GridView1" runat="server"></asp:GridView>
            </div>
        </form>
    </body>
    </html>
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            string constr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand("SELECT Field1,  Field2, Field3 FROM Table_1"))
                {
                    using (SqlDataAdapter sda = new SqlDataAdapter())
                    {
                        cmd.Connection = con;
                        sda.SelectCommand = cmd;
                        using (DataTable dt = new DataTable())
                        {
                            sda.Fill(dt);
                            GridView1.DataSource = dt;
                            GridView1.DataBind();
                            DropDownList1.DataSource = dt;
                            DropDownList1.DataBind();
                            DropDownList1.DataValueField = "Field2";
                            DropDownList1.DataTextField = "Field1";
                            DropDownList1.DataBind();
                            DropDownList1.Items.Insert(0, "Select");
                        }
                    }
                }
            }
        }
    }
    

    User's image

    User's image

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful