开发者

Access child label control value in Datalist control with multiple records when child LinkButton control is clicked?

开发者 https://www.devze.com 2023-03-18 19:42 出处:网络
I have one aspx page with a Datalist control and a FormView control. The Datalist displays all the records and the FormView is hidden. The FormView displays the details of a record and the Datalist co

I have one aspx page with a Datalist control and a FormView control. The Datalist displays all the records and the FormView is hidden. The FormView displays the details of a record and the Datalist control is hidden. When I click a record in the Datalist I go to the Formview to display, edit the selected record.

How can I pass the record id to the click event to select the record in the database a开发者_高级运维nd display the details in the Formview?

There are multiple controls with the same client id and the runtime appends a number to the end to make the client id unique.

Here is the control I want to access from code behind:

<asp:Label ID="lHideAndSeek" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "IdString") %>' CssClass="hideAndSeek">

Here is the control I use to select the company details:

<asp:LinkButton ID="btnSelectCompany" runat="server" CausesValidation="false"
                    onclick="btnSelectCompany_Click"><%# DataBinder.Eval(Container.DataItem, "Name") %></asp:LinkButton>

Here is the code behind where I want to access the label control when the linkbutton is clicked and pass the value to the SelectCompany() instead of hard coding the 4:

protected void btnSelectCompany_Click(object sender, EventArgs e)
{
    DataList1.Visible = false;

    FormView1.ChangeMode(FormViewMode.ReadOnly);

    using (jpEntities myEntities = new jpEntities())
    {
        FormView1.DataSource = myEntities.SelectCompany(4);
        FormView1.DataBind();
    }

    FormView1.Visible = true;
}

Thanks you for looking at this!


You can add a CommandArgument attribute to your LinkButton and bind it's data, like you did with Text attribute. Then you can extract that value from code-behind.

Something like this.

<asp:LinkButton ID="btnSelectCompany" runat="server" 
            CausesValidation="false" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "Id") %>' OnClick="btnSelectCompany_Click"><%# DataBinder.Eval(Container.DataItem, "Name") %></asp:LinkButton>

LinkButton linkButton = sender as LinkButton;
int id = int.Parse(linkButton.CommandArgument);
0

精彩评论

暂无评论...
验证码 换一张
取 消