none
关于SelectedItemTemplate模板 RRS feed

  • 问题

  • 请问各位大侠,如何在ListView的SelectedItemTemplate模板中改变Label的text属性。
    youl
    • 已移动 孟宪会Moderator 2009年8月19日 1:15 (发件人:.NET Framework 一般性问题讨论区)
    2009年8月18日 23:43

答案

  • 你好,
    如果你要编辑记录,我觉得用GridView可能要更方便。另外如果成绩库和你说的注册库不是同一张表。那你就不能用DataSourceID这样的属性了。
    你可以参考下面的例子试试。
    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.GridView1.DataSource = this.GetSource;
                this.GridView1.DataBind();
            }
    
        }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            this.GridView1.EditIndex = e.NewEditIndex;
            this.GridView1.DataSource = this.GetSource;
            this.GridView1.DataBind();
        }
        protected string connectionStr = ConfigurationManager.ConnectionStrings["test_EmployeeInfo_SKConnectionString"].ConnectionString;
    
    
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            string imageName = ((this.GridView1.Rows[e.RowIndex].FindControl("tbImageName") as TextBox)).Text;
            string des = ((this.GridView1.Rows[e.RowIndex].FindControl("tbDes") as TextBox)).Text;
            string path = ((this.GridView1.Rows[e.RowIndex].FindControl("tbPath") as TextBox)).Text;
            string catetoryID = ((this.GridView1.Rows[e.RowIndex].FindControl("tbCategoryId") as TextBox)).Text;
            //insert into database
        }
        protected DataTable GetSource
        {
            get
            {
                DataTable dt = new DataTable();
                if (ViewState["dt"] == null)
                {
                    SqlConnection scon = new SqlConnection(connectionStr);
                    SqlCommand scom = scon.CreateCommand();
                    scom.CommandText = "SELECT Id, ImageName, Des, Path, CategoryId FROM Images";
                    SqlDataAdapter sda = new SqlDataAdapter(scom);
                    sda.Fill(dt);
                }
                else return ViewState["dt"] as DataTable;
    
                return dt;
            }
        }
    </script>
    
     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns=False  AllowSorting="True"
                DataKeyNames="Id" 
                onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating">
                
                <Columns>
                    <asp:CommandField ShowEditButton="True" />
                  
                    <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" 
                        ReadOnly="True" SortExpression="Id" runat="server" />
                    <asp:TemplateField>
                        <ItemTemplate>
                        <%#Eval("ImageName") %>
                        </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="tbImageName" runat="server" Text='<%#Eval("ImageName") %>'></asp:TextBox>
                    </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField>
                    
                        <ItemTemplate>
                       <%#Eval("Des") %>
                        </ItemTemplate>
                        <EditItemTemplate>
                        <asp:TextBox ID="tbDes" runat="server" Text='<%#Eval("Des") %>'></asp:TextBox>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    
                    <asp:TemplateField>
                    
                        <ItemTemplate>
                       <%#Eval("Path") %>
                        </ItemTemplate>
                        <EditItemTemplate>
                        <asp:TextBox ID="tbPath" runat="server" Text='<%#Eval("Path") %>'></asp:TextBox>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    
                     <asp:TemplateField>
                    
                        <ItemTemplate>
                          <%#Eval("CategoryId") %>
                        </ItemTemplate>
                        <EditItemTemplate>
                        <asp:TextBox ID="tbCategoryId" runat="server" Text='<%#Eval("CategoryId") %>'></asp:TextBox>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    
                  
                   
                </Columns>
            </asp:GridView>


    Microsoft Online Community Support
    2009年8月20日 5:47

全部回复

  • 你好,你可以在  SelectedItemTemplate 中重新定义 Label 的 Text 属性。如下:

    <asp:Label ID="BondKeyLabel" runat="server" Text='<%# "已选中" + Eval("BondKey") %>' />


    知识改变命运,奋斗成就人生!
    2009年8月19日 1:15
    版主
  •      <asp:ListView ID="ListView1" runat="server" DataKeyNames="id" 
                DataSourceID="SqlDataSource1">
                <ItemTemplate>
                    <tr style="">
                        <td>
                            <asp:Label ID="idLabel" runat="server" Text='<%# Eval("id") %>' />
                            <asp:Button ID="Button2" runat="server" Text="Button"  CommandName="Select"/>    </td>
            
                    </tr>
                </ItemTemplate>
    
                <LayoutTemplate>
                    <table runat="server">
                        <tr runat="server">
                            <td runat="server">
                                <table ID="itemPlaceholderContainer" runat="server" border="0" style="">
                                    <tr runat="server" style="">
                                        <th runat="server">
                                            id</th>
                                    
                                    </tr>
                                    <tr ID="itemPlaceholder" runat="server">
                                    </tr>
                                </table>
                            </td>
                        </tr>
                        <tr runat="server">
                            <td runat="server" style="">
                            </td>
                        </tr>
                    </table>
                </LayoutTemplate>
                <EditItemTemplate>
                    <tr style="">
                        <td>
                            <asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="更新" />
                            <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="取消" />
                        </td>
                        <td>
                            <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("id") %>'></asp:TextBox> </td>
              
                    </tr>
                </EditItemTemplate>
           <SelectedItemTemplate>
           
               <tr style="">
                   <td>
                       <asp:Label ID="idLabel" runat="server" Text='<%# Eval("id") %>' />
                       <asp:Button ID="Button1" runat="server" Text="edit" CommandName="Edit" /> </td>
                 
           
           </SelectedItemTemplate>
            </asp:ListView>
    2009年8月19日 2:55
  • 这是创建的一个新的,我想改变原来。
    youl
    2009年8月19日 7:08
  • 你描述问题太抽象了 没人看的懂
    2009年8月19日 7:10
  • 问题是这样的:
          通过一定的条件在SQL成绩库中筛选出一部分人并在ListView1中显示出来。然后在ListView1中选择其中1人,将其部分字段的值和另外需要添加的信息插入到SQL注册库中,并在ListView2中显示出来。
         请各位大师赐教。谢谢!!
    youl
    2009年8月20日 1:13
  • 你好,
    如果你要编辑记录,我觉得用GridView可能要更方便。另外如果成绩库和你说的注册库不是同一张表。那你就不能用DataSourceID这样的属性了。
    你可以参考下面的例子试试。
    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.GridView1.DataSource = this.GetSource;
                this.GridView1.DataBind();
            }
    
        }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            this.GridView1.EditIndex = e.NewEditIndex;
            this.GridView1.DataSource = this.GetSource;
            this.GridView1.DataBind();
        }
        protected string connectionStr = ConfigurationManager.ConnectionStrings["test_EmployeeInfo_SKConnectionString"].ConnectionString;
    
    
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            string imageName = ((this.GridView1.Rows[e.RowIndex].FindControl("tbImageName") as TextBox)).Text;
            string des = ((this.GridView1.Rows[e.RowIndex].FindControl("tbDes") as TextBox)).Text;
            string path = ((this.GridView1.Rows[e.RowIndex].FindControl("tbPath") as TextBox)).Text;
            string catetoryID = ((this.GridView1.Rows[e.RowIndex].FindControl("tbCategoryId") as TextBox)).Text;
            //insert into database
        }
        protected DataTable GetSource
        {
            get
            {
                DataTable dt = new DataTable();
                if (ViewState["dt"] == null)
                {
                    SqlConnection scon = new SqlConnection(connectionStr);
                    SqlCommand scom = scon.CreateCommand();
                    scom.CommandText = "SELECT Id, ImageName, Des, Path, CategoryId FROM Images";
                    SqlDataAdapter sda = new SqlDataAdapter(scom);
                    sda.Fill(dt);
                }
                else return ViewState["dt"] as DataTable;
    
                return dt;
            }
        }
    </script>
    
     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns=False  AllowSorting="True"
                DataKeyNames="Id" 
                onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating">
                
                <Columns>
                    <asp:CommandField ShowEditButton="True" />
                  
                    <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" 
                        ReadOnly="True" SortExpression="Id" runat="server" />
                    <asp:TemplateField>
                        <ItemTemplate>
                        <%#Eval("ImageName") %>
                        </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="tbImageName" runat="server" Text='<%#Eval("ImageName") %>'></asp:TextBox>
                    </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField>
                    
                        <ItemTemplate>
                       <%#Eval("Des") %>
                        </ItemTemplate>
                        <EditItemTemplate>
                        <asp:TextBox ID="tbDes" runat="server" Text='<%#Eval("Des") %>'></asp:TextBox>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    
                    <asp:TemplateField>
                    
                        <ItemTemplate>
                       <%#Eval("Path") %>
                        </ItemTemplate>
                        <EditItemTemplate>
                        <asp:TextBox ID="tbPath" runat="server" Text='<%#Eval("Path") %>'></asp:TextBox>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    
                     <asp:TemplateField>
                    
                        <ItemTemplate>
                          <%#Eval("CategoryId") %>
                        </ItemTemplate>
                        <EditItemTemplate>
                        <asp:TextBox ID="tbCategoryId" runat="server" Text='<%#Eval("CategoryId") %>'></asp:TextBox>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    
                  
                   
                </Columns>
            </asp:GridView>


    Microsoft Online Community Support
    2009年8月20日 5:47