当前位置:编程学习 > C#/ASP.NET >>

急急~~在线等

本人新手- -才学ASP.NET~~遇到了个问题困扰我很久了!
需要使用ASP.NET AJAX 在GRIDVIEW1 模板化一个按钮 选择一行 将选中行的的一个值存入SESSION中 当单击这个按钮时,会出现
GRIDVIEW2!GRIDVIEW2中的数据源通过SESSION中的值来选取 也就是说选择不同的行会出现不同的结果 我也把GRIDVIEW2放在UPDATEPANEL中了但是总是说什么引用的对象未被实例化!以下是代码- -!
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" OnRowCommand="GridView1_OnRowCommand" 
                        ForeColor="#333333" GridLines="None" Width="611px" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" Height="163px">
                        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                        <Columns>
                       
               
                        <asp:BoundField DataField="**" HeaderText="**" />
                        <asp:BoundField DataField="**" HeaderText="**" />
                         <asp:CommandField ShowSelectButton="true" />
                        
                        </Columns>
                        
            <RowStyle BackColor="#EFF3FB" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#2461BF" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                <asp:GridView ID="GridView2" runat="server" Height="145px" Width="610px" OnSelectedIndexChanged="GridView2_SelectedIndexChanged" CellPadding="4" ForeColor="#333333" GridLines="None">
                    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                    <Columns>
                    <asp:BoundField DataField="**" HeaderText="**" />
                    <asp:BoundField DataField="**" HeaderText="*" />
                    <asp:BoundField DataField="**" HeaderText="**" />
                    <asp:BoundField DataField="**" HeaderText="**" />
                    </Columns>
                    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                    <EditRowStyle BackColor="#999999" />
                    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                </asp:GridView>
                 
                <br />
                <asp:TextBox ID="TextBox4" runat="server" Height="86px" Width="603px"></asp:TextBox><br />
                <asp:TextBox ID="TextBox3" runat="server" Height="118px" Width="603px" OnTextChanged="TextBox3_TextChanged"></asp:TextBox><br />
                <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /> 
                <br />
            </ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="GridView1" EventName="SelectedIndexChanged" />
                <asp:AsyncPostBackTrigger ControlID="GridView1" EventName="PageIndexChanged" />
                <asp:AsyncPostBackTrigger ControlID="GridView1" EventName="Sorted" />
            </Triggers>
        </asp:UpdatePanel>

后台代码在这里!
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;
public partial class studentpage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       Label1.Text =Convert.ToString(Session["**"]);
        Label2.Text = Convert.ToString(Session["**"]);
        if (!IsPostBack)
        {
            bind();
          
        }
        
    }

    private void bind1()
    {
        string sql1 = @"select **,**,**,** from ** where **='" + Convert.ToString(Session["**"]) + "'";
        SqlConnection conn1 = new SqlConnection(@"**");
        SqlDataAdapter da1 = new SqlDataAdapter(sql1, conn1);
        DataSet ds1=new DataSet();
        da1.Fill(ds1,"**");
        GridView2.DataSource=ds1;
        GridView2.DataBind();
        conn1.Close();
    }


    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Response.Redirect("********.aspx");
    }

    public void bind()
    {
        string sql = @"select * from class where **='"+Convert.ToString(Session["**"])+"'";
        SqlConnection conn = new SqlConnection(@"**");
        SqlDataAdapter da = new SqlDataAdapter(sql, conn);
        DataSet ds = new DataSet();
        da.Fill(ds, "**”);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        conn.Close();
    }
    protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
    {
        
    }
   
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void GridView1_OnRowCommand(object sender, GridViewCommandEventArgs e)
    {

        Session["**"] = GridView1.SelectedRow.Cells[0];
         bind1();
        this.UpdatePanel1.Update();

    
        
    }

    protected void TextBox3_TextChanged(object sender, EventArgs e)
    {
        
        
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
       
       
    } 
}
急急~~在线等- -!希望大家不吝赐教!!

--------------------编程问答-------------------- 友情帮顶 --------------------编程问答-------------------- Session["**"] = GridView1.SelectedRow.Cells[0]; 这块出了问题。不应该是这样取值的 --------------------编程问答-------------------- 那应该怎么取值?  <asp:CommandField ShowSelectButton="true" /> 这里~~ --------------------编程问答-------------------- 汗啊汗~!- -!没人么 --------------------编程问答-------------------- Session["**"] = GridView1.SelectedRow.Cells[0].Text.ToString();  --------------------编程问答-------------------- 为什么不在里面绑定一个DATAKEYS,你既然是做传值,那那个传的值数据库里面肯定有吧~ --------------------编程问答-------------------- 恩是有。。但是现在一点击选择按钮就出现 "引用的对象未被实例化"这个很恼火 --------------------编程问答-------------------- 你如果用RowCommand的话,应该设一下CommandArgument,这个来获得你这一列的唯一键值。。比如说有ID,这个CommandArgument可设为ID值,再根据这个ID值查找详细信息显示在GridView2。。 --------------------编程问答-------------------- ~~~~~~~~~~~~~~~~~~ --------------------编程问答-------------------- 这个我晓得,问题是现在那个按钮根本没法用 点了就说"引用的对象未被实例化 --------------------编程问答-------------------- gridview.row[(int)e.CommandArgument].cells[0].text --------------------编程问答--------------------
引用 10 楼 cctvzj8888 的回复:
这个我晓得,问题是现在那个按钮根本没法用 点了就说"引用的对象未被实例化

你的写法根本取不到那一行,肯定会报错啊。。 --------------------编程问答-------------------- 你写的方法有问题根本得不到你想要的那一行。
当选中后你可以得到选中行的id 号码,就是表的主键,然后可以再数据库的表中在找到选中的那个id 的那一行,在绑定到你的那个gridview2中就可以了。
--------------------编程问答-------------------- 呃 能不能说下具体的写法? --------------------编程问答--------------------
引用 11 楼 ivws_19 的回复:
gridview.row[(int)e.CommandArgument].cells[0].text

出现 “指定的转换无效”
还有就是- -!从GRIDVIEW中取出来的不是数字 而是一个字符串。。 --------------------编程问答-------------------- - -!!! --------------------编程问答-------------------- gridview.row[(int)e.CommandArgument].cells[0].Text.ToString(); 
补充:.NET技术 ,  ASP.NET
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,