question

RAVI-6930 avatar image
0 Votes"
RAVI-6930 asked LanHuang-MSFT commented

girdiview binding wrong value

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


dotnet-aspnet-general
kkk.png (96.1 KiB)
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Your code sample is functioning exactly as written. Unfortunately, the code does not make any logical sense and is overly complex which makes understanding the intent difficult.

I recommend learning how to use the Visual Studio debugger to follow the logical flow of the code. Compare the actual execution flow to how you expect the code to work. That should help you figure out where your expectations do not match the logical flow.

First look at the Visual Studio Debugger

For example, you've configured the dropdown list to auto post back which does not execute any code because the load event's conditional logic is only looking for a GET not a POST. There's other questionable logic related to the T1 textbox, datatable, GridView, etc....

Please debug your code.


0 Votes 0 ·

Can you remove the existing codes as much as possible, make a minimum sample only to show the issue you are wondering, and explain your issue in detail based on the minimum sample?

0 Votes 0 ·

Can you remove the existing codes as much as possible, make a minimum sample only to show the issue you are wondering, and explain your issue in detail based on the minimum sample?

The design does not make a lot of sense. Can you explain the use case?

0 Votes 0 ·

The efforts to create minimum sample makes a lot of sense as he will be able to find the cause during such efforts and probably to find the solution. IMO, it is a common practice. Don't you think so?

If he could not find the cause by himself regardless of the efforts he can post the minimum sample and request us to find the cause and solution. It will be much easier for us to review the minimum codes. I guess nobody wants see the codes such as the empty event handler, style-related properties and others which do not cause the issue.

0 Votes 0 ·
LanHuang-MSFT avatar image
0 Votes"
LanHuang-MSFT answered LanHuang-MSFT edited

Hi @RAVI-6930,
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.



1.jpg (134.5 KiB)
10.gif (170.1 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RAVI-6930 avatar image
0 Votes"
RAVI-6930 answered LanHuang-MSFT commented

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

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @RAVI-6930,
I've edited the answer, you can check it out.
If you have additional questions about this answer, click "Comment".194999-1.jpg
Best regards,
Lan Huang


0 Votes 0 ·
1.jpg (119.9 KiB)

Hello

Can You Please Help Me To Solve This Issues Without postback it has to show what i select my dropdownlist will have same value for multiple names i need small code with example please

Thanking You

0 Votes 0 ·

Hi @RAVI-6930,
I've put the demo in the answer, I just made a slight change to your code.
I have explained in my answer that no two items can have the same value.
Best regards,
Lan Huang

0 Votes 0 ·