【解決方法】リストから選択したレコードを同じページに移動するにはどうすればよいですか


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>

——————

そのようなコードがあります。 リストから選択したレコードを同じページに移動できません。 の hfcustomer 変数は値を受け取りません。 何らかの理由でコードを修正するにはどうすればよいですか?

私が試したこと:

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();
        }
    }
}
} 

解決策 1

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>

コメント

タイトルとURLをコピーしました