本文介绍了使用链接buuton在网格视图中单击,在另一个网格视图中显示选定的行.,我在这里没有使用任何ID.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<asp:TemplateField HeaderText="Travels Name">
            <ItemTemplate>
               <asp:LinkButton ID="lb1" runat="server" Height="30px" Width="120px"

                    Text='<%# Eval("TRVLSNAME") %>' OnClick ="lb1_Click"></asp:LinkButton>
                 </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Bus Type">
            <ItemTemplate>
                <asp:LinkButton ID="LinkButton4" runat="server" Height="30px" Width="120px" PostBackUrl="~/Seat_Selection.aspx" Text='<%# Eval("Bus_Type") %>'></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Departure Time">
            <ItemTemplate>
                <asp:LinkButton ID="LinkButton5" runat="server" Height="30px" Width="120px"  PostBackUrl="~/Seat_Selection.aspx" Text='<%# Eval("Bus_Timing") %>'></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Fare">
            <ItemTemplate>
                <asp:LinkButton ID="LinkButton6" runat="server" Height="30px" Width="120px" PostBackUrl="~/Seat_Selection.aspx" Text='<%# Eval("Bus_Fair") %>'></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>

推荐答案

 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlDataAdapter da = new SqlDataAdapter("select * from BusesInfo", con);
        DataTable dt = new DataTable();
        da.Fill(dt);
        GridView1.DataSource = dt;
        DataBind();

    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string examinationid = GridView1.SelectedValue.ToString();
        SqlDataAdapter da = new SqlDataAdapter("select busName, No,fromtime,totime from Buses where BusNo=" + BusNo, con);
        DataTable dt = new DataTable();
        da.Fill(dt);
        GridView2.DataSource = dt;
        DataBind();
        GridView1.Visible = false;
    }
    protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridView2.Visible = true;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        GridView1.Visible = true;
    }
}


Aspx页面的代码:


code for Aspx Page:

<asp:gridview id="GridView1" runat="server" autogenerateselectbutton="True" xmlns:asp="#unknown">
        DataKeyNames="BusNo"
        onselectedindexchanged="GridView1_SelectedIndexChanged">
    </asp:gridview>
    <br />
    <asp:gridview id="GridView2" runat="server" xmlns:asp="#unknown">
        onselectedindexchanged="GridView2_SelectedIndexChanged">
    </asp:gridview>


public void lb1_Click(object sender, EventArgs e)
{
 GridViewRow clickedRow = ((LinkButton)sender).NamingContainer as GridViewRow;
        Label lbl1 = (Label)clickedRow.FindControl("lbl1");
  string TavelsName = clickedRow.Cells[0].Text;
sql connection()=new SqlConnection();
con.open();
sqlcommand cmd=new sqlcommand("Select BusName,BusType from Buses Where TravelsName='"+TravelsName+"'",con);
sqlDataAdapter da=new sqlDataAdapter(cmd);
Dataset ds=new dataSet();
Da.Fill(ds);
GridView2.DataSource=ds;
GridView2.DataBind();
cmd.executeNonQuery();
con.close();
}


这篇关于使用链接buuton在网格视图中单击,在另一个网格视图中显示选定的行.,我在这里没有使用任何ID.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 10:28