[英]Using linkbutton with gridview and updatepanel
Iam ASP.Net初学者,我已经开始了一个项目,在该项目中,我使用了带有gridview的一列中的linkbuttons从数据库中获取数据。 这个想法是,只要我们单击链接按钮,就会出现一个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(); }
}
}
发生这种情况是因为您已将同一列(已绑定第一个链接按钮的列)绑定到第二个链接按钮。 将单独的列绑定到第二链接按钮。 希望我的回答对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.