使用带有gridview和updatepanel的链接按钮

时间:2015-04-30 13:22:43

标签: c# asp.net gridview

Iam ASP.Net初学者,我已经开始了一个项目,我在一个列中使用带有链接按钮的Gridview从DB中获取数据。我们的想法是,每当我们点击链接按钮时,都会出现一个CSS弹出窗口,其中包含一些文本框/区域,其中包含有关该主题的信息。我遇到的问题是,当我点击第一个链接按钮时,我得到与第一个主题相关的信息,当我点击第二个链接按钮时,我再次得到相同的信息。我不知道如何克服这个问题。而且我注意到第一次linkbutton2不起作用,只有在打开linkbtn1细节后才能工作。

这是Gridview标记代码块以及从数据库中获取数据的代码。

ASPX页面:

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <asp:DropDownList ID="DropDownList1" runat="server" CssClass="ddl-pri" Font-Names="Helvetica World"
            Font-Size="15pt" Height="40px" Width="155px" Style="margin-top: -64px; margin-left: 172px; position: absolute;"
            AutoPostBack="True" DataSourceID="DropDownList"
            DataTextField="Project_Name" DataValueField="Project_Name"
            AppendDataBoundItems="True"
            OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
            <asp:ListItem Selected="True">Select</asp:ListItem>
        </asp:DropDownList>

        <asp:SqlDataSource ID="DropDownList" runat="server"
            ConnectionString="<%$ ConnectionStrings:PECS %>"
            SelectCommand="SELECT [Project Name] AS Project_Name FROM [Project]"></asp:SqlDataSource>
        <div id="grid">
            <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"
                GridLines="None" AutoGenerateColumns="False" AllowPaging="True"
                PageSize="8" OnRowCommand="grdListUsers_RowCommand"
                OnRowDataBound="RowDataBound">
                <AlternatingRowStyle BackColor="White" />
                <Columns>
                    <asp:TemplateField HeaderText="#">
                        <ItemTemplate>
                            #
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Subject">
                        <ItemTemplate>
                            <asp:LinkButton ID="LinkButton1" runat="server" Text='<%# Eval("Subject").ToString().Shorten(10)%>'
                                CommandName="LinkButton1"></asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Estimated Start">
                        <ItemTemplate>
                            <asp:Label ID="Label10" runat="server" Text='<%# Eval("Est Start", "{0:d}") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Estimated End">
                        <ItemTemplate>
                            <asp:Label runat="server" Text='<%# Eval("Est End", "{0:d}") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Estimated Time">
                        <ItemTemplate>
                            <asp:Label ID="Label16" runat="server" Text='<%# Eval("Estd Time") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="Progress" HeaderText="Progress"
                        SortExpression="Progress" />
                    <asp:BoundField DataField="Type" HeaderText="Type"
                        SortExpression="Type" />
                    <asp:BoundField DataField="Assigned" HeaderText="Employee Name"
                        SortExpression="Assigned" />
                    <asp:BoundField DataField="Role" HeaderText="Role" SortExpression="Role" />
                    <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
                    <asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" />
                </Columns>
                <EditRowStyle BackColor="#2461BF" />
                <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White"
                    Font-Names="NewsGoth BT" Font-Size="13.38pt" />
                <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                <RowStyle BackColor="#EFF3FB" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                <SortedAscendingCellStyle BackColor="#F5F7FB" />
                <SortedAscendingHeaderStyle BackColor="#6D95E1" />
                <SortedDescendingCellStyle BackColor="#E9EBEF" />
                <SortedDescendingHeaderStyle BackColor="#4870BE" />
            </asp:GridView>
            <asp:SqlDataSource ID="GridViewSource1" runat="server"
                ConnectionString="<%$ ConnectionStrings:PECS %>"
                SelectCommand="SELECT Task.Subject, Task.[Est Start], Task.[Est End], Task.[Estd Time], Task.Progress, Task.Type, Task.Assigned, Task.[Project Name], Employee.Role, Employee.Email, Employee.Phone FROM Task INNER JOIN Employee ON Task.Assigned = Employee.Name WHERE (Task.[Project Name] = 'Temp')"></asp:SqlDataSource>
        </div>
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="DropDownList1" EventName="SelectedIndexChanged" />
    </Triggers>
</asp:UpdatePanel>

C#代码隐藏:

protected void Page_Load(object sender, EventArgs e)
{
    this.RegisterPostBackControl();
    string ddl = DropDownList1.Text;
    AddProjBut.Style.Add("display", "none");

    if (Session["UserName"] != null)
    {
        Userlbl.Text = Session["UserName"].ToString();
    }
    else
    {
        Response.Redirect("~/Content/Intro.aspx");
    }

    Calendar1.Style.Add("position", "absolute");
    Calendar2.Style.Add("position", "absolute");
    Calendar3.Style.Add("position", "absolute");
    Calendar4.Style.Add("position", "absolute");

    if (!IsPostBack)
    {
        //  AddProjBut.Style.Add("display", "none");
        Calendar1.Visible = false;
        Calendar2.Visible = false;
        Calendar3.Visible = false;
        Calendar4.Visible = false;
        TextBox1.Text = "";
        TextBox2.Text = "";
        EstdStart.Text = "";
        EstdEnd.Text = "";
        Hours.Text = "";
    }
    else
    {
        style();
    }
}

private void RegisterPostBackControl()
{
    foreach (GridViewRow row in GridView1.Rows)
    {
        LinkButton lnkFull = row.FindControl("LinkButton1") as LinkButton;
        ScriptManager.GetCurrent(this).RegisterPostBackControl(lnkFull);
    }
}

protected void grdListUsers_RowCommand(object sender, GridViewCommandEventArgs e)
{

    if (e.CommandName == "LinkButton1")
    {
        GridViewRow row = (GridViewRow)((Control)e.CommandSource).NamingContainer;

        LinkButton lb = (LinkButton)row.FindControl("LinkButton1");

        if (DropDownList1.SelectedIndex > 0)
        {
            using (SqlConnection con = new SqlConnection(cs))
            {
                SqlCommand cmd = new SqlCommand();
                con.Open(); cmd.Connection = con;

                if (DropDownList1.SelectedIndex > 0)
                {
                    cmd.CommandText = "SELECT Subject, [Task Desc], [Est Start], [Est End], [Estd Time], Progress, Type, Assigned FROM Task WHERE (Subject = @txt)";
                    cmd.Parameters.AddWithValue("@txt", lb.Text);

                    using (SqlDataReader rdr = cmd.ExecuteReader())
                    {
                        DataTable dt = new DataTable();

                        while (rdr.Read())
                        {
                            TextBox3.Text = rdr["Subject"].ToString();
                            TextBox4.Text = rdr["Task Desc"].ToString();
                            TEstsStart.Text = rdr["Est Start"].ToString();
                            TEstdEnd.Text = rdr["Est End"].ToString();
                            TaskHours.Text = rdr["Estd Time"].ToString();
                            Progress.Text = rdr["Progress"].ToString();
                            DropDownList2.Text = rdr["Type"].ToString();
                            Assign.Text = rdr["Assigned"].ToString();
                        }
                    }
                }
                else
                {
                    lblmsg.Visible = true;
                    lblmsg.Text = "Select Some Value";
                    style();
                }
            }
        }
        else { style(); }
    }
}

1 个答案:

答案 0 :(得分:0)

它的发生是因为你将同一列(你已经与第一个链接按钮绑定的列)绑定到第二个链接按钮。将单独的列绑定到第二个链接按钮。希望我的回答对你有帮助。