girdiview binding wrong value

RAVI 896 Reputation points
2022-04-20T12:26:05.89+00:00

Hello

<script>
$(function () {
$(".mytext").val($(".myddl").val())
$(".myddl").change(function () {
$(".mytext").val($(this).val())

            })  
  
  
        })  
    </script>  

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

This Is my c# code

using System;  
using System.Data;  
using System.Configuration;  
using System.Collections;  
using System.Web;  
using System.Web.Security;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
using System.Web.UI.WebControls.WebParts;  
using System.Web.UI.HtmlControls;  
  
public partial class testtest : System.Web.UI.Page  
{  
    protected void Page_Load(object sender, EventArgs e)  
    {  
        if (!Page.IsPostBack)  
        {  
               
            BindGridView();  
        }  
    }  
  
  
    private void BindGridView()  
    {  
        //Declare a datatable for the gridview  
        DataTable dt = new DataTable();  
        //Add Columns to the datatable  
  
        dt.Columns.Add("F1");  
        dt.Columns.Add("F2");  
          
  
        // dt.Columns.Add("PONO");  
  
  
        //Define a datarow for the datatable dt  
        DataRow dr = dt.NewRow();  
  
        //Now add the datarow to the datatable  
        dt.Rows.Add(dr);  
        //Now bind the datatable to gridview  
        GridView1.DataSource = dt;  
        GridView1.DataBind();  
        //Now hide the extra row of the grid view  
        GridView1.Rows[0].Visible = false;  
        //Delete row 0 from the datatable  
        dt.Rows[0].Delete();  
        dt.AcceptChanges();  
        //View the datatable to the viewstate  
        ViewState["FFFData"] = dt;  
    }  
  
    protected void Button3_Click(object sender, EventArgs e)  
    {  
        DataTable dt = new DataTable();  
  
        dt = (DataTable)ViewState["FFFData"];  
  
        DataRow dr = dt.NewRow();  
  
        dr["F1"] = DropDownList2.SelectedItem.Text;  
        dr["F2"] = T1.Text;  
         
        dt.Rows.Add(dr);  
  
  
        //Now bind the datatable to the gridview  
        GridView1.DataSource = dt;  
        GridView1.DataBind();  
  
        ViewState["FFFData"] = dt;  
        GridView1.Visible = true;  
  
        T1.Text = "";  
  
    }  
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)  
    {  
  
    }  
}  
  

When i run page if i select b then on button click its binding A only in grid view how to solve this issue

194676-kkk.png

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

2 answers

Sort by: Most helpful
  1. Lan Huang-MSFT 25,386 Reputation points Microsoft Vendor
    2022-04-21T05:51:14.75+00:00

    Hi @RAVI ,
    The reason for this problem is because you set the ListItem values of A and B to be the same.
    ListItem.Value Property gets or sets the value associated with the ListItem.
    You need to set ListItem.Value to a different value.

    <asp:ListItem Value="1">A</asp:ListItem>  
     <asp:ListItem Value="3">B</asp:ListItem>  
     <asp:ListItem Value="5">C</asp:ListItem>  
     <asp:ListItem Value="7">D</asp:ListItem>   
    

    EDIT
    No two items can have the same value. When there are multiple options with the same value,
    it can't tell which value you want, so it assumes the first one. Best practice is to use unique option values.
    Maybe you can use ListItem.Text to set the same.

    <asp:ListItem Text="1">A</asp:ListItem>  
    <asp:ListItem Text="1">B</asp:ListItem>  
    <asp:ListItem Text="5">C</asp:ListItem>  
    <asp:ListItem Text="7">D</asp:ListItem>  
    

    DEMO
    You can refer to the code, I only made changes in the places circled in red.
    195564-1.jpg
    195487-10.gif
    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 comments No comments

  2. RAVI 896 Reputation points
    2022-04-21T07:33:34.333+00:00

    Hello

    The data will be same in that case what to do how to change my javascript code to bind those what user select

    thanks