ASP.NET和C#:gridview选择不起作用

huangapple go评论56阅读模式
英文:

ASP.NET & C# : gridview select does no action

问题

GridView1_SelectedIndexChanged 为什么不起作用?

我自学ASP.NET(这意味着我通过YouTube学习),使用VS 2015。

我创建了一个项目列表,并希望在文本框中显示所选项目,但代码没有做出任何更改。

我创建了一个空项目,添加了一个主页和默认页面,然后只添加了一个文本框、网格、ScriptManager、UpdatePanel和ContentTemplate。

无论我点击网格上的任何一行的选择按钮,文本框仍然为空。

英文:

Why is GridView1_SelectedIndexChanged not working?

I'm learning ASP.NET on my own (which means I learn through youtube), using VS 2015.

I created a grid of items and want to show the selected item in a textbox, but code doesn't do any change.

I created an empty project, added a masterpage and default page, and just added a text and grid, scriptmanager updatepanel contenttemplate.

Whenever I click select on any row from the grid textbox is still empty.

MasterPage.master:

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <asp:ContentPlaceHolder id="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
        
        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>

Default.aspx:

<%@ Page Title="" enableeventvalidation="true" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
    <style type="text/css">
        .auto-style1 {
            width: 100%;
        }
    </style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <table class="auto-style1">

                <tr>
                    <td>
                        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                    </td>
                    
                </tr>
                <tr>
                    <td> </td>
                    <td> </td>
                </tr>
                <tr>
                    <td>
                        <asp:GridView ID="GridView1" runat="server" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
                            <Columns>
                                <asp:CommandField ButtonType="Button" ShowSelectButton="True" />
                            </Columns>
                        </asp:GridView>
                    </td>
                    
                </tr>
            </table>

        </ContentTemplate>
    </asp:UpdatePanel>
</asp:Content>

Default.aspx.cs:

using System;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable tbl = new DataTable();
            tbl.Columns.Add("serial");

            for (int i = 0; i < 4; i++)
            {
                DataRow row = tbl.NewRow();
                row[0] = i + 1;
                tbl.Rows.Add(row);
            }
            GridView1.DataSource = tbl;
            GridView1.DataBind();
        }
        
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        TextBox1.Text = GridView1.SelectedRow.Cells[0].Text;
    }
}

答案1

得分: 1

你在SelectedIndexChanged方法中选择了错误的单元格。

第一个单元格(索引0)包含按钮,所以你需要第二个单元格(索引1)。

你应该在该方法上设置一个断点,自己查看GridView中填充了哪些其他属性。

英文:

You are selecting the wrong cell on the SelectedIndexChanged method.

The first Cell (index 0) contains the button, so you want the second cell (index 1).

You should place a breakpoint on the method and see for yourself what other properties are filled in the GridView.

huangapple
  • 本文由 发表于 2023年2月6日 10:32:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75356873.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定