How I can access to the “Text” Property of a ListView LayoutTemplate Control

梦想与她 提交于 2019-12-13 04:35:23

问题


I have a ASP.NET Application and use a ListView. This ListView has a LayoutTemplate,ItemTemplate and a AlternatingItemTemplate. In My LayoutTemplate I have LinkLabels. I want to make a Multilanguage Page and I must access to The Text Property of this Linklabels. But I don't can access to this Property.

My ASPX (ListView)

<LayoutTemplate>
    <table id="UserTable" runat="server" border="0">
        <tr id="Tr1" style="background-color:#E5E5FE">
            <th runat="server"><asp:LinkButton ID="lnkBenutzer" runat="server" >id_Benutzer</asp:LinkButton></th>
            <th runat="server"><asp:LinkButton ID="lnkemail" runat="server" >id_Email</asp:LinkButton></th>
            <th runat="server"><asp:LinkButton ID="lnkVorname" runat="server" >id_Vorname</asp:LinkButton></th>
            <th runat="server"><asp:LinkButton ID="lnkNachname" runat="server" >id_Nachname</asp:LinkButton></th>
            <th runat="server"><asp:LinkButton ID="lnkTelefon" runat="server" >id_Telefon</asp:LinkButton></th>
        </tr>
        <tr runat="server" id="ItemPlaceholder">
        </tr>
    </table>
</LayoutTemplate>

<ItemTemplate>

    <tr> 

        <td align="left" ><asp:Label ID="Label1" Text='<%# Eval("Benutzername") %>' runat="server" /></td>
        <td align="left"><asp:Label ID="Label2" Text='<%# Eval("eMail") %>' runat="server" /></td>
        <td align="left"><asp:Label ID="Label3" Text='<%# Eval("Vorname") %>' runat="server" /></td>
        <td align="left"><asp:Label ID="Label4" Text='<%# Eval("Nachname") %>' runat="server" /></td>
        <td align="left"><asp:Label ID="Label5" Text='<%# Eval("Telefonnummer") %>' runat="server" /></td>

     <td align="left"><asp:Button ID="Button1" Text="Anzeigen" OnCommand="Button1_Command" CommandName="Select" CommandArgument='<%# Container.DataItemIndex %>'  runat="server" /></td>

      <td align="left"><asp:Label ID="Label6" Text='<%# Eval("GUID") %>' runat="server" Visible="False" /></td>


    </tr>

</ItemTemplate>

<AlternatingItemTemplate>

    <tr style="background-color:#EFEFEF"> 

        <td align="left" ><asp:Label ID="Label1" Text='<%# Eval("Benutzername") %>' runat="server" /></td>
        <td align="left"><asp:Label ID="Label2" Text='<%# Eval("eMail") %>' runat="server" /></td>
        <td align="left"><asp:Label ID="Label3" Text='<%# Eval("Vorname") %>' runat="server" /></td>
        <td align="left"><asp:Label ID="Label4" Text='<%# Eval("Nachname") %>' runat="server" /></td>
        <td align="left"><asp:Label ID="Label5" Text='<%# Eval("Telefonnummer") %>' runat="server" /></td>

    <td align="left"><asp:Button ID="Button1" Text="Anzeigen" OnCommand="Button1_Command" CommandName="Select" CommandArgument='<%# Container.DataItemIndex %>'  runat="server" /></td>

        <td align="left"><asp:Label ID="Label6" Text='<%# Eval("GUID") %>' runat="server" Visible="False" /></td>

    </tr>

</AlternatingItemTemplate>

My C# code:

        if (lang == "de")
        {
            string xmlfile= "de_language.xml"; 
                              //GetXMLElement() is my Method
            id_SearchUser.Text = GetXMLElement(xmlfile, "id_SearchUser");
            id_location.Text = GetXMLElement(xmlfile, "id_location");
            Button2.Text = GetXMLElement(xmlfile, "id_Search");

                        //I must access to the text property
            //lnkBenutzer.text = GetXMLElement(xmlfile, "User"); 
        }

My Button:

protected void Button1_Command(object sender, CommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                //Der Index der Zeile wird ermitellt
                int index = Convert.ToInt32(e.CommandArgument);

                string xmlfile = Session["language_file"].ToString(); 
               //it doen't works :( 
                ((LinkButton)myListView.FindControl("lnkBenutzer")).Text = GetXMLElement(xmlfile, "id_User");

                //Der Inhalt (GUID[Unsichtbar]) wird aus der zeile mit dem Index herausgezogen und übergeben       

                Label lb = (Label)myListView.Items[index].FindControl("Label6");

                //Der GUID, Benutzer-Nachname und das Werk werden an eine Session übergeben
                Session["UserID"] = lb.Text;
                Session["SearchUser"] = txtBenutzer.Text;
                Session["DropDownValue"] = dropWerk.SelectedValue;

                //Es wird eine neue Webform geöffnet
                Response.Redirect("Benutzer.aspx");

            }

        }

回答1:


I believe you need to do something like the following:

In your Benutzer.aspx:

public void Page_Load() {
    string xmlfile = Session["language_file"].ToString(); 
    LinkButton lnkBenutzer = (LinkButton)myListView.FindControl("lnkBenutzer");
    lnkBenutzer.Text = GetXMLElement(xmlfile, "User"); 
}

In short what you need to do is "find" the list view control and then find the control inside the list view. This could be shortened to:

((LinkButton)myListView.FindControl("lnkBenutzer")).Text = GetXMLElement(xmlfile, "User");

Going further you could extract this to a method wherein you pass in your item name and the new text :

public void SetText(string controlName, string text)
{
    ((LinkButton)myListView.FindControl(controlName)).Text = text;
}

then you could call:

SetText("lnkBenutzer", GetXMLElement(xmlfile, "User"));

replacing your names and vales as required.




回答2:


I think it'll be something like this in codeBehind:

LinkButton lb = myListView.FindControl("lnkBenutzer") as LinkButton;
lb.Text = GetXMLElement(xmlfile, "user");

Haven't tested it though



来源:https://stackoverflow.com/questions/11792799/how-i-can-access-to-the-text-property-of-a-listview-layouttemplate-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!