如何使用数据列表中的下拉列表更改数据列表中的标签值? [英] How to change label value in datalist using dropdownlist in datalist?

查看:73
本文介绍了如何使用数据列表中的下拉列表更改数据列表中的标签值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友,
我有一个静态绑定的数据列表控件,其中包含带有mrp的产品列表以及我们的价格标签,该价格标签显示了数据库中商品模板中的价格.我还具有一个下拉列表,该下拉列表位于价格标签旁边.Dropdownlist包含产品重量(例如1kg,2kg, 3毫升等)我已经生成下拉选择索引更改事件.现在我想根据选择的drpdownlist值更改mrp和我们的价格标签值.请帮帮我.以下是我的代码.

Hello frnds,
I have datalist control statically binded which contains list of products with mrp and our price label showing prices in item template from database.I also have dropdownlist in datalist which is present next to price labels.Dropdownlist contains weight of products(like 1kg,2kg,3ml,etc.)I have generated dropdown selectionindex changed event.Now i want to change mrp and our price label value according to the selected value of drpdownlist.Please help me.Following below is my code.

 <asp:DataList ID="DataList1" runat="server" DataKeyField="pid" 

            DataSourceID="SqlDataSource1" RepeatDirection="Horizontal" 

                        RepeatColumns="4" CellSpacing="0" 

        style="position: relative; top: 0px; left: 0px;" 

        onitemcommand="DataList1_ItemCommand" 

        onitemcreated="DataList1_ItemCreated" onitemdatabound="DataList1_ItemDataBound" 

       >
            <itemtemplate>
            <div class="prod_box">
               <div class="center_prod_box">        
                <div class="product_title"><a href="products-details.aspx?id=<%# Eval(" pid=") %>" style="text-decoration:none;">
                 <font color="#da2924"><%# Eval("company_name")%>   <%# Eval("sub_catagory")%></font></a></div>
             <div class="product_img"><a href="products-details.aspx?id=<%# Eval(" pid=") %>" style="text-decoration:none;">
               <asp:Image ID="product_imageLabel" runat="server"  ImageUrl='<%# Eval("image") %>'  Height="120px" Width="130px"  /></a>
               </div>
            
                   <%--<asp:Label ID="Label1"  runat="server" Text="MRP:" Font-Size="9.5px">--%>
                <asp:Label ID="mrp_priceLabel" runat="server" Font-Size="12px" Text='<%# Eval("mrp_price","{0:##0.00}") %>' />
               <asp:Label ID="Label4"  runat="server" Text="Rs" Font-Size="12px">
              
             
                 
               <%-- <asp:Label ID="Label2"  runat="server" Text="OUR PRICE:" Font-Size="9.5px"> --%>
                <asp:Label ID="our_priceLabel" runat="server" Font-Size="12px"  Font-Bold=true ForeColor="#28156d" Text='<%# Eval("our_price","{0:##0.00}") %>' />
                  <asp:Label ID="Label3" Font-Size="12px"  runat="server" Font-Bold=true ForeColor="#28156d" Text="Rs" />
                <br />
               <asp:Label ID="Label5" runat="server" Text='<%#Eval("sub_catagory") %>' Visible="false">
              <%--  <asp:Button ID="btnAdd" runat="server" Width="80px" OnClick="btnAdd_Click" 
                        Text="Add to Cart" /><br />--%>
                   <asp:DropDownList ID="DropDownList1" runat="server" 

                    a AppendDataBoundItems="true" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"

                >                   
                       <asp:Label ID="Label1"  runat="server" Text="Qty" Font-Size="12px">
                       <asp:TextBox ID="TextBox1" runat="server" Width="30px">                  
           </div>

           <div class="prod_details_tab" style="margin-removed30px;">                                          
               <asp:ImageButton ID="ImageButton1" ImageUrl="~/images/btn-add-to-cart.jpg" runat="server" 

                   CommandArgument='<%# Eval("pid") %>' CommandName="AddtoCart"/>
            </div>
           </div>
<br /><br />
            </itemtemplate>
                  
         <asp:SqlDataSource ID="SqlDataSource1" runat="server" 

            ConnectionString="<%$ ConnectionStrings:royalstationersConnectionStringOK %>" 

            SelectCommand="select distinct T.pid,T.catagory,T.sub_catagory, T.image,
 T.company_name, T.weight,T.mrp_price,T.our_price,T.featuredescription
from tbl_product T
where T.image = (select max(image)
from tbl_product
where sub_catagory = T.sub_catagory) order by T.pid desc">
                               
         <asp:SqlDataSource ID="SqlDataSourceddl" runat="server" 

                       ConnectionString="<%$ ConnectionStrings:royalstationersConnectionStringOK %>" 

                       SelectCommand="select * from tbl_product " >
                       
       <%-- <SelectParameters>
            <asp:ControlParameter ControlID="DataList1" Name="sub_catagory" PropertyName="'<%# Eval("sub_catagory") %>'" Type="String" />
        </SelectParameters>
                       --%>


背后的代码:-


Code Behind:-

protected void DataList1_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item ||
            e.Item.ItemType == ListItemType.AlternatingItem)
        {
            string str = ConfigurationManager.ConnectionStrings["RoyalStationersConnectionString"].ConnectionString.ToString();
            // Retrieve the Label control in the current DataListItem.
            Label lbls = (Label)e.Item.FindControl("Label5");

            DropDownList ddl = (DropDownList)e.Item.FindControl("DropDownList1");
            //ddl.Items.Add(lbls.Text);
            string sql = "select productcode,mrp_price,our_price,weight from tbl_product where sub_catagory='" + lbls.Text + "' order by mrp_price desc";
            SqlConnection objconn = new SqlConnection(str);
            SqlCommand objcmd = new SqlCommand(sql, objconn);
            objconn.Open();
            SqlDataReader rd = objcmd.ExecuteReader();

            ddl.DataValueField = "productcode";
            ddl.DataTextField = "weight";
            ddl.DataSource = rd;
            ddl.DataBind();
            //while (rd.Read())
            //{

            //    //Label lbl1 = (Label)e.Item.FindControl("mrp_priceLabel");
            //    //lbl1.Text = rd["mrp_price"].ToString();
            //    ddl.Items.Add(rd["productcode"].ToString());
            //    ddl.DataValueField = rd["productcode"].ToString();
            //    ddl.DataTextField = rd["weight"].ToString();
            //    ddl.DataBind();
             
            //}                   
        }
    }


 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
}

推荐答案

ConnectionStrings:royalstationersConnectionStringOK%>" span> =" 选择不同的T.pid,T.catagory,T.sub_catagory,T.image, T.company_name,T.weight,T.mrp_price,T.our_price,T.featuredescription 来自tbl_product T 其中T.image =(选择max(image) 来自tbl_product 其中sub_catagory = T.sub_catagory)由T.pid desc排序". > < asp:SqlDataSource ID =" runat 服务器" span> ConnectionString="<%
ConnectionStrings:royalstationersConnectionStringOK %>" SelectCommand="select distinct T.pid,T.catagory,T.sub_catagory, T.image, T.company_name, T.weight,T.mrp_price,T.our_price,T.featuredescription from tbl_product T where T.image = (select max(image) from tbl_product where sub_catagory = T.sub_catagory) order by T.pid desc"> <asp:SqlDataSource ID="SqlDataSourceddl" runat="server" ConnectionString="<%


ConnectionStrings:royalstationersConnectionStringOK %>" SelectCommand="select * from tbl_product " > <%-- <SelectParameters> <asp:ControlParameter ControlID="DataList1" Name="sub_catagory" PropertyName="'<%# Eval("sub_catagory") %>'" Type="String"/> </SelectParameters> --%>
ConnectionStrings:royalstationersConnectionStringOK %>" SelectCommand="select * from tbl_product " > <%-- <SelectParameters> <asp:ControlParameter ControlID="DataList1" Name="sub_catagory" PropertyName="'<%# Eval("sub_catagory") %>'" Type="String" /> </SelectParameters> --%>


Code Behind:-


Code Behind:-

protected void DataList1_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item ||
            e.Item.ItemType == ListItemType.AlternatingItem)
        {
            string str = ConfigurationManager.ConnectionStrings["RoyalStationersConnectionString"].ConnectionString.ToString();
            // Retrieve the Label control in the current DataListItem.
            Label lbls = (Label)e.Item.FindControl("Label5");

            DropDownList ddl = (DropDownList)e.Item.FindControl("DropDownList1");
            //ddl.Items.Add(lbls.Text);
            string sql = "select productcode,mrp_price,our_price,weight from tbl_product where sub_catagory='" + lbls.Text + "' order by mrp_price desc";
            SqlConnection objconn = new SqlConnection(str);
            SqlCommand objcmd = new SqlCommand(sql, objconn);
            objconn.Open();
            SqlDataReader rd = objcmd.ExecuteReader();

            ddl.DataValueField = "productcode";
            ddl.DataTextField = "weight";
            ddl.DataSource = rd;
            ddl.DataBind();
            //while (rd.Read())
            //{

            //    //Label lbl1 = (Label)e.Item.FindControl("mrp_priceLabel");
            //    //lbl1.Text = rd["mrp_price"].ToString();
            //    ddl.Items.Add(rd["productcode"].ToString());
            //    ddl.DataValueField = rd["productcode"].ToString();
            //    ddl.DataTextField = rd["weight"].ToString();
            //    ddl.DataBind();
             
            //}                   
        }
    }


 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
}


I would implement jquery and ajax in order to do this to avoid constant postbacks of the entire page.
You would create a selectedIndexChanged handler in jquery that would then do an ajax call back to a WebMethod on the server. That WebMethod would return a JSON result including the updated values that you want. You then just update the appropriate html element values in the success method. It will make your page interaction seem very slick to the end user as well.
I would implement jquery and ajax in order to do this to avoid constant postbacks of the entire page.
You would create a selectedIndexChanged handler in jquery that would then do an ajax call back to a WebMethod on the server. That WebMethod would return a JSON result including the updated values that you want. You then just update the appropriate html element values in the success method. It will make your page interaction seem very slick to the end user as well.


这篇关于如何使用数据列表中的下拉列表更改数据列表中的标签值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆