locked
if same value its taking top value why RRS feed

  • Question

  • User-807418713 posted

    Hello

    this is my code

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

    In the above code myddl i have diffeerent value and the mytext  will have same price nowwhen ever postabck happn it goes to top record why its happening

    Saturday, March 13, 2021 2:03 PM

All replies

  • User475983607 posted

    We cannot see you markup.  If the markup has more than one .mytext then the code is functioning as expected.  Learn jQuery selectors and use the browser dev tools to debug your code. 

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

    Saturday, March 13, 2021 2:57 PM
  • User1535942433 posted

    Hi Gopi.MCA,

    According to your description and codes,I don't understand your requirement clearly.I'm guessing that your problem is when you post back,the dropdownlist always select first item. If your problem is this,you could remove the Auto postback=true.Just like this:

    protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostback())
                 {
                        myddl.DataBind();
                  }
        }

    If your issue isn't like this,you could post full codes and more details of requirement to us.It will help us to solve your problems.

    Best regards,

    Yijing Sun

    Monday, March 15, 2021 3:29 AM
  • User-807418713 posted

    Hello

    This is my sql table data

     ***** Object:  Table [dbo].[Personal_Details]    Script Date: 03/17/2021 10:30:46 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    SET ANSI_PADDING ON
    GO
    CREATE TABLE [dbo].[Personal_Details](
    	[ID] [int] IDENTITY(1,1) NOT NULL,
    	[ItemName] [nchar](10) NULL,
    	[Rate] [varchar](50) NULL,
     CONSTRAINT [PK_Personal_Details] PRIMARY KEY CLUSTERED 
    (
    	[ID] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    GO
    SET ANSI_PADDING OFF
    GO
    SET IDENTITY_INSERT [dbo].[Personal_Details] ON
    INSERT [dbo].[Personal_Details] ([ID], [ItemName], [Rate]) VALUES (1, N'ITEM1     ', N'10')
    INSERT [dbo].[Personal_Details] ([ID], [ItemName], [Rate]) VALUES (2, N'ITEM2     ', N'44')
    INSERT [dbo].[Personal_Details] ([ID], [ItemName], [Rate]) VALUES (3, N'ITEM3     ', N'16')
    INSERT [dbo].[Personal_Details] ([ID], [ItemName], [Rate]) VALUES (4, N'ITEM4     ', N'10')
    INSERT [dbo].[Personal_Details] ([ID], [ItemName], [Rate]) VALUES (5, N'ITEM5     ', N'20')
    INSERT [dbo].[Personal_Details] ([ID], [ItemName], [Rate]) VALUES (6, N'ITEM6     ', N'100')
    INSERT [dbo].[Personal_Details] ([ID], [ItemName], [Rate]) VALUES (7, N'ITEM7     ', N'90')
    INSERT [dbo].[Personal_Details] ([ID], [ItemName], [Rate]) VALUES (8, N'ITEM8     ', N'100')
    INSERT [dbo].[Personal_Details] ([ID], [ItemName], [Rate]) VALUES (9, N'ITEM9     ', N'110')
    INSERT [dbo].[Personal_Details] ([ID], [ItemName], [Rate]) VALUES (10, N'ITEM10    ', N'10')
    SET IDENTITY_INSERT [dbo].[Personal_Details] OFF
    

    This is my aspx page

    <%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" Title="Untitled Page" %>
    
    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
      <script>
            $(function () {
                $(".mytext").val($(".myddl").val())
                $(".myddl").change(function () {
                    $(".mytext").val($(this).val())
    
                })
    
    
            })
        </script> 
        
       
        Select Item<asp:DropDownList ID="DropDownList1" CssClass="myddl" runat="server" DataTextField="ItemName" DataValueField="Rate">
        </asp:DropDownList><br />
        Rate<asp:TextBox ID="TextBox1" CssClass="mytext" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="ADD Record" /><br />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellSpacing="6"
            EmptyDataText="No Details Found !" Font-Bold="True" Font-Names="Palatino Linotype"
            Font-Size="12pt" 
            ShowFooter="True">
            <Columns>
                <asp:TemplateField HeaderText=" Name">
                    <ItemTemplate>
                        <asp:Label ID="L1" runat="server" Text='<%#Eval("Name") %>'></asp:Label>
                    </ItemTemplate>
                    <HeaderStyle ForeColor="Red" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Rate">
                    <ItemTemplate>
                        <asp:Label ID="L2" runat="server" Text='<%#Eval("Rate") %>'></asp:Label>
                    </ItemTemplate>
                    <HeaderStyle ForeColor="Red" />
                </asp:TemplateField>
               
                <asp:CommandField ShowDeleteButton="True">
                    <HeaderStyle ForeColor="Red" />
                </asp:CommandField>
            </Columns>
        </asp:GridView>
        <br />
        <br />
    </asp:Content>
    
    

    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;
    using System.Data.SqlClient;
    using System.Collections.Specialized;
    using System.Text;
    using System.Drawing;
    using System.IO;
    using System.Net;
    using System.Net.Mail;
    using System.Net.Configuration;
    public partial class Default4 : System.Web.UI.Page
    {
    
        private void BindGridView()
        {
            //Declare a datatable for the gridview
            DataTable dt = new DataTable();
            //Add Columns to the datatable
            dt.Columns.Add("Name");
            dt.Columns.Add("Rate");
          
    
            // 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["KKJJHHVVJJ"] = dt;
        }
    
    
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                DataTable dt = new DataTable();
                SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["CHEMIMSConnectionString"].ConnectionString);
                con1.Open();
                SqlCommand cmd1 = new SqlCommand("select ItemName,Rate from Personal_Details", con1);
                SqlDataAdapter ada1 = new SqlDataAdapter(cmd1);
                ada1.Fill(dt);
                DropDownList1.DataTextField = "ItemName";
                DropDownList1.DataValueField = "Rate";
                DropDownList1.DataSource = dt;
                DropDownList1.DataBind();
                con1.Close();
                con1.Dispose();
    
    
                BindGridView();
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
    
            dt = (DataTable)ViewState["KKJJHHVVJJ"];
    
            DataRow dr = dt.NewRow();
    
            dr["Name"] = DropDownList1.SelectedItem.Text;
            dr["Rate"] = TextBox1.Text;
           
            
                //Add the datarow to the datatable
                dt.Rows.Add(dr);
                //Now bind the datatable to the gridview
                GridView1.DataSource = dt;
                GridView1.DataBind();
    
    
                ViewState["KKJJHHVVJJ"] = dt;
    
    
            GridView1.Visible = true;
    
           
    
        }
    }
    

    now on load i select ITEM4 and then i click add button it goes to ITEM1..

    How to solve this

    Wednesday, March 17, 2021 5:04 AM
  • User1535942433 posted

    Hi Gopi.MCA,

    According to your description and codes,I suggest that the DataValueField value must be unique just like Id.

    When you have multiple same DataValueField,it will find multiple different DataTextField. So,it will get the top text according the order defaultly.

    Best regards,

    Yijing Sun

    Thursday, March 18, 2021 7:35 AM
  • User-807418713 posted

    Hi

    How to retain selcted value of drodpwnlist if postabck happen

     <script>
            $(function () {
                $(".mytext").val($(".myddl").val())
                $(".myddl").change(function () {
                    $(".mytext").val($(this).val())
    
                })
    
    
            })
        </script> 
    Thursday, March 18, 2021 8:19 AM
  • User-807418713 posted

    Hello

    Please Help To Solve This...?

    Thanking You

    Friday, March 19, 2021 6:24 AM
  • User1535942433 posted

    Hi Gopi.MCA,

    According to your description,I tested your codes.And the way is right.However,do you have get the dropdownlist's value?

    According to your codes,you need to write like this:

    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
        <%--    <script type="text/javascript" src="Scripts/chosen.jquery.min.js"></script>--%>
        <script src="Scripts/jquery-3.2.1.min.js"></script>
        <script type="text/javascript">
            $(function () {
                $("input[id*=TextBox1]").val($("select[id*=DropDownList1] option:selected").val())
                $("select[id*=DropDownList1]").change(function () {
                    $("input[id*=TextBox1]").val($(this).val())
                })
            })
        </script>
    
    
        Select Item<asp:DropDownList ID="DropDownList1" CssClass="chzn-select" runat="server" DataTextField="ItemName" DataValueField="Rate" Width="185px">
        </asp:DropDownList><br />
        Rate<asp:TextBox ID="TextBox1" CssClass="mytext" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="ADD Record" /><br />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellSpacing="6"
            EmptyDataText="No Details Found !" Font-Bold="True" Font-Names="Palatino Linotype"
            Font-Size="12pt"
            ShowFooter="True">
            <Columns>
                <asp:TemplateField HeaderText=" Name">
                    <ItemTemplate>
                        <asp:Label ID="L1" runat="server" Text='<%#Eval("Name") %>'></asp:Label>
                    </ItemTemplate>
                    <HeaderStyle ForeColor="Red" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Rate">
                    <ItemTemplate>
                        <asp:Label ID="L2" runat="server" Text='<%#Eval("Rate") %>'></asp:Label>
                    </ItemTemplate>
                    <HeaderStyle ForeColor="Red" />
                </asp:TemplateField>
                <asp:CommandField ShowDeleteButton="True">
                    <HeaderStyle ForeColor="Red" />
                </asp:CommandField>
            </Columns>
        </asp:GridView>
        <br />
        <br />
    </asp:Content>

    Code-behind:

    private void BindGridView()
            {
                //Declare a datatable for the gridview
                DataTable dt = new DataTable();
                //Add Columns to the datatable
                dt.Columns.Add("Name");
                dt.Columns.Add("Rate");
    
    
                // 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["KKJJHHVVJJ"] = dt;
            }
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack)
                {
                    DataTable dt = new DataTable();
                    SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["aspnet-TestApplicationWithDatabase-20190820030542"].ConnectionString);
                    con1.Open();
                    SqlCommand cmd1 = new SqlCommand("select ItemName,Rate from Personal_Details", con1);
                    SqlDataAdapter ada1 = new SqlDataAdapter(cmd1);
                    ada1.Fill(dt);
                    DropDownList1.DataTextField = "ItemName";
                    DropDownList1.DataValueField = "Rate";
                    DropDownList1.DataSource = dt;
                    DropDownList1.DataBind();
                    
                    con1.Close();
                    con1.Dispose();
                    BindGridView();
                }
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "", "window.onload = function(){ showDropdown(document.getElementById('DropDownList1'));}", true);
                DataTable dt = new DataTable();
    
                dt = (DataTable)ViewState["KKJJHHVVJJ"];
    
                DataRow dr = dt.NewRow();
    
                dr["Name"] = DropDownList1.SelectedItem.Text;
                dr["Rate"] = TextBox1.Text;
    
    
                //Add the datarow to the datatable
                dt.Rows.Add(dr);
                //Now bind the datatable to the gridview
                GridView1.DataSource = dt;
                GridView1.DataBind();
    
    
                ViewState["KKJJHHVVJJ"] = dt;
    
    
                GridView1.Visible = true;
    
    
            }

    Result:

    Best regards,

    Yijing Sun

    Friday, March 19, 2021 6:53 AM
  • User-807418713 posted

    Hello

    for example Dropdownoist1 has Item1 as 10 and Item4 has 10

    Then if i select Item4 and i click add its showing Item1 in gridview automticallly

    thats the error

    Friday, March 19, 2021 7:27 AM
  • User1535942433 posted

    Hi Gopi.MCA,

    I have said,you need to set the dropdownlist's DataValueField as a unique value.You could bind Id intead of Rate to the DataValueField.

    And then,you could select Rate based on Id and DataTextField using sql query.Finally,you could fill the result to textbox.

    Best regards,

    Yijing Sun

    Friday, March 19, 2021 8:47 AM
  • User-807418713 posted

    can i have one xample is it possible

    Thanks 

    Thanks lot

    Friday, March 19, 2021 8:55 AM
  • User-807418713 posted

    Hello

    Good Day

    Whenever we select itemname it should bind rate only based on that item

    any example thanks

    Monday, March 22, 2021 9:01 AM
  • User1535942433 posted

    Hi Gopi.MCA,

    As far as I think,there are no way to get the selected value to add in the gridview if the DataValueField have multiple same value.You could use ID as the DataValueField.Just like this:

     Select Item<asp:DropDownList ID="DropDownList1" CssClass="chzn-select" runat="server" DataTextField="ItemName" DataValueField="Rate" Width="185px">
        </asp:DropDownList><br />
        Rate<asp:TextBox ID="TextBox1" CssClass="mytext" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="ADD Record" /><br />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellSpacing="6"
            EmptyDataText="No Details Found !" Font-Bold="True" Font-Names="Palatino Linotype"
            Font-Size="12pt"
            ShowFooter="True">
            <Columns>
                <asp:TemplateField HeaderText=" Name">
                    <ItemTemplate>
                        <asp:Label ID="L1" runat="server" Text='<%#Eval("Name") %>'></asp:Label>
                    </ItemTemplate>
                    <HeaderStyle ForeColor="Red" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Rate">
                    <ItemTemplate>
                        <asp:Label ID="L2" runat="server" Text='<%#Eval("Rate") %>'></asp:Label>
                    </ItemTemplate>
                    <HeaderStyle ForeColor="Red" />
                </asp:TemplateField>
                <asp:CommandField ShowDeleteButton="True">
                    <HeaderStyle ForeColor="Red" />
                </asp:CommandField>
            </Columns>
        </asp:GridView>

    Code-behind:

    private void BindGridView()
            {
                //Declare a datatable for the gridview
                DataTable dt = new DataTable();
                //Add Columns to the datatable
                dt.Columns.Add("Name");
                dt.Columns.Add("Rate");
                // 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["KKJJHHVVJJ"] = dt;
            }
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack)
                {
                    DataTable dt = new DataTable();
                    SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["aspnet-TestApplicationWithDatabase-20190820030542"].ConnectionString);
                    con1.Open();
                    SqlCommand cmd1 = new SqlCommand("select ID, ItemName,Rate from Personal_Details", con1);
                    SqlDataAdapter ada1 = new SqlDataAdapter(cmd1);
                    ada1.Fill(dt);
                    DropDownList1.DataTextField = "ItemName";
                    DropDownList1.DataValueField = "ID";
                    DropDownList1.DataSource = dt;
                    DropDownList1.DataBind();
                    con1.Close();
                    con1.Dispose();
                    BindGridView();
                }
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                //ScriptManager.RegisterStartupScript(this, this.GetType(), "", "window.onload = function(){ showDropdown();}", true);
                DataTable dt = new DataTable();
    
                dt = (DataTable)ViewState["KKJJHHVVJJ"];
    
                DataRow dr = dt.NewRow();
                SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["aspnet-TestApplicationWithDatabase-20190820030542"].ConnectionString);
                con1.Open();
                SqlCommand cmd1 = new SqlCommand("select Rate from Personal_Details where ItemName='"+ DropDownList1.SelectedItem.Text + "'", con1);
                SqlDataReader myReader = cmd1.ExecuteReader();
                var x="";
                while (myReader.Read())
                {
                    // Assuming your desired value is the name as the 3rd field
                    x = myReader["Rate"].ToString();
                }
                dr["Name"] = DropDownList1.SelectedItem.Text;
                dr["Rate"] = x;
    
                //Add the datarow to the datatable
                dt.Rows.Add(dr);
                //Now bind the datatable to the gridview
                GridView1.DataSource = dt;
                GridView1.DataBind();
    
    
                ViewState["KKJJHHVVJJ"] = dt;
    
    
                GridView1.Visible = true;
    
    
            }

    Best regards,

    Yijing Sun

    Tuesday, March 23, 2021 8:34 AM