Click here to Skip to main content
15,846,006 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
ASP.NET
%@ Page Language="C#" AutoEventWireup="true" 
CodeFile="Default6.aspx.cs" Inherits="Default6" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    
    <style type="text/css">
        .style3
        {
            height: 26px;
        }
        .style4
        {
            height: 26px;
            width: 144px;
        }
    </style>
    
</head>
<body>
    <form id2="form1" runat="server">
    <div>
  <asp:HiddenField ID="hfCustomerId" runat="server" />
     <table style="width: 1000px">
    <% foreach (var c in this.item())
        { %>
    <tr>
        <td class="style3">
            <label><%=c.countryid%></label></td>
        <td class="style3">
            <input type="text" 
                value='<%=c.countryname %>' /></td>
        <td class="style3">
            <input type="text" value='<%=c.photo%>' />
            </td>
        <td class="style3">
            <input type="radio" <%=c.countryname== "india" ? 
            "checked" : "" %> /></td>
            
        <td class="style3">
               <input type="checkbox" <%=c.countryname!= "india" ? 
               "checked" : "" %> /></td>     
         
          <td class="style4">  
         <a href='countrydetail.aspx?idno=<%=c.countryid 
         %>'>country detaıl</a> </td>
         
           <td>  
              <asp:Button ID="Button1" runat="server" Text="Button" 
            BackColor="#FF6699" Width="500px" onclick="ON" />
     </tr>
    <% } %>
   
</table>
    </div>    
    
   <hr />
<div>
    <table>
        <tr>
            <td>
                <label><%=countryidx%></label>
            </td>
        </tr>
        <tr>
            <td>
                <input type="text" value='<%=ulkeismix%>' />
            </td>
        </tr>
        <tr>
            <td>
                <input type="text" value='<%=photox%>' />
            </td>
        </tr>
    </table>
</div>
    
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>
<script type="text/javascript">
    $(function () {
        $('.View').on('click', function () {
            var id = $(this).closest('tr').find('td').eq(0).text().trim();
            $('[id*=hfCustomerId]').val(id);
        });
    });
</script>
    </form>
</body>
</html>


------------------

I have such a code. I cannot bring the record I have selected from the list to the same page. The hfcustomer variable does not receive any value. For some reason, how can I fix the code?

What I have tried:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient; 

public partial class Default6 : System.Web.UI.Page
{    
    public int countryidx;
    public string photox;
    public string ulkeismix;

    public List<Country> item()
    {
        string sqlStatment = "select * from Country";
        string constr = System.Configuration.ConfigurationManager.
                        ConnectionStrings["constr"].ConnectionString;
        using (System.Data.SqlClient.SqlConnection con = 
               new System.Data.SqlClient.SqlConnection(constr))
        {
            using (System.Data.SqlClient.SqlCommand cmd = 
             new System.Data.SqlClient.SqlCommand(sqlStatment, con))
            {
                cmd.Connection.Open();
                System.Data.SqlClient.SqlDataReader reader = 
                       cmd.ExecuteReader();
                List<Country> emp = new List<Country>();
                while (reader.Read())
                {
                    Country country = new Country();
                    country.countryid = Convert.ToInt32(reader.GetValue(0));
                    country.countryname = reader.GetValue(2).ToString();
                    country.photo = reader.GetValue(1).ToString();
                    emp.Add(country);
                }

                reader.Close();
                cmd.Connection.Close();
                return emp;
            }
        }
    }

    public class Country
    {
        public int countryid { get; set; }
        public string countryname { get; set; }
        public string photo { get; set; }
    }

    public List<Country> Item { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            if (!this.IsPostBack)
            {
                this.Item = this.item();
            }
        }
    }

    protected void ON(object sender, EventArgs e)
    {
    string id = Request.Form[hfCustomerId.UniqueID].Trim();
    string sqlStatment = "SELECT * FROM  country WHERE countryid=@Id";
    string constr = 
    System.Configuration.ConfigurationManager.
    ConnectionStrings["constr"].ConnectionString;
    using (System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(constr))
    {
        using (System.Data.SqlClient.SqlCommand cmd = 
               new System.Data.SqlClient.SqlCommand(sqlStatment, con))
        {
            cmd.Parameters.AddWithValue("@Id", id);
            cmd.Connection.Open();
            System.Data.SqlClient.SqlDataReader reader = cmd.ExecuteReader();
            if (reader.Read())
            {
              countryidx = Convert.ToInt32(reader.GetValue(0));
                ulkeismix= reader.GetValue(1).ToString();
                photox = reader.GetValue(2).ToString();
            }

            reader.Close();
            cmd.Connection.Close();
        }
    }
}
} 
Posted
Updated 3-Oct-23 7:25am
v4
Comments
[no name] 3-Oct-23 12:07pm    
string id = "2" If I do it like this, it works without any problems, that is, if I give any id number
[no name] 3-Oct-23 12:09pm    
string id = Request.Form[hfCustomerId.UniqueID].Trim();Why is this sentence not working
string id="2" //This sentence works but I don't want to change idno every now and then.
Ralf Meier 4-Oct-23 12:05pm    
have you ever took a look with the DEBUGGER what your Request delivers you ?

1 solution

The problem is solved, the error is due to the missing button click sentence, it should be written like this, the code is completely correct.

 <td>
                <asp:Button ID="btnView" CssClass="View" runat="server" Text="View" OnClick="ON" />
            </td>
            
     </tr>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900