动态Gridview,下拉选择. [英] Dynamic Gridview, Drop down Selection.

查看:89
本文介绍了动态Gridview,下拉选择.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些有关如何进入下一阶段并改善现有问题的指导.基本上,我有一个数据库,通过下拉菜单进行选择时,将使用三个不同但相关的数据.我需要通过txtBoxNumber对lblRate *进行计算以获得lblTotal. lblRate根据下拉选择进行更改.目前,根据选择,我有三个不可见的gridview.我想知道是否有更好的方法可以做到这一点.

请告知您是否可以在下面进行编码:

I need some guidance as to how to approach the next phase and improve my existing problem. Basically I have a database and on selection through a drop down three differing but related pieces of data will be used. I need to do a calculation on the lblRate * by the txtBoxNumber to get the lblTotal. lblRate changes according to the dropdown selection. Currently I have three gridviews that are visible invisible according to the selection. I wonder if there is a better way to do this.

Please advise if you can Code below:

namespace gridView
{
    public partial class Default : System.Web.UI.Page
    {
        OleDbConnection con;    
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                GridView1.Visible = true;
                GridView2.Visible = false;
                GridView3.Visible = false;                              
            }
            catch (OleDbException ex)
            {
                ex.Message.ToString();
            }
        }
       
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lblItemDescription = (Label)e.Row.FindControl("lblItemDescription");
                Label lblRate = (Label)e.Row.FindControl("lblRate");
                Double PR = Convert.ToDouble(lblRate.Text);
                TextBox txtBoxNumber = (TextBox)e.Row.FindControl("txtBoxNumber");
                Label lblTotal = (Label)e.Row.FindControl("lblTotal");
                txtBoxNumber.Attributes.Add("onchange", "multiply(" + PR + ", '" + txtBoxNumber.ClientID + "','" + lblTotal.ClientID +  "')");
            }                                  
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {         
            if(DropDownList1.SelectedValue == "Platinum")
            {
                GridView1.Visible = true;
                GridView2.Visible = false;
                GridView3.Visible = false;                
            }
            else if (DropDownList1.SelectedValue == "Gold")
            {                
                GridView1.Visible = false;
                GridView2.Visible = true;
                GridView3.Visible = false;
                   
            }
            else if (DropDownList1.SelectedValue == "Silver")
            {
                GridView1.Visible = false;
                GridView2.Visible = false;
                GridView3.Visible = true;              
            }
        }
    }
}





<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="gridView.Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

<script type="text/javascript">
    function multiply(Rate, number, total) {
        var num = parseFloat(document.getElementById(number).value);
        var tot = document.getElementById(total);
        var totValue = parseFloat(((Rate * num)));
        var totValueRound = Math.round(totValue, 4);
        var totValueRound = totValue;
        tot.innerHTML = totValueRound;        
    }
</script>
</head>
<body>
<form id="form1" runat="server">
    <div>   
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 

            DataSourceID="AccessDataSource2" DataTextField="supportLevel" 

            DataValueField="supportLevel" onselectedindexchanged="DropDownList1_SelectedIndexChanged"  

            >
        </asp:DropDownList>

        <asp:AccessDataSource ID="AccessDataSource2" runat="server" 

            DataFile="~/App_Data/supportServices.accdb" 

            SelectCommand="SELECT [supportLevel] FROM [supportLevel]"></asp:AccessDataSource>

        <asp:AccessDataSource ID="AccessDataSource1" runat="server" 

            DataFile="~/App_Data/supportServices.accdb" 

            SelectCommand="SELECT * FROM [supportDevicesAndServices]" 

            >
        </asp:AccessDataSource>       
       
        <asp:GridView ID="GridView1" runat="server"

AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound" 

            DataSourceID="AccessDataSource1" Visible="false" EnableViewState="False">
            <Columns>
            <asp:TemplateField HeaderText="Devices And Services" Visible="True">
                    <ItemTemplate>
                        <asp:Label ID="lblItemDescription" runat="server"

Text='<%# Eval("itemDescription") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Rate">
                    <ItemTemplate>
                        <asp:Label ID="lblRate" runat="server" Text='<%# Eval("platinumRate") %>' ></asp:Label>
                       
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Number">
                    <ItemTemplate>
                        <asp:TextBox ID="txtBoxNumber" Width="100px"

runat="Server" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Total">
                    <ItemTemplate>
                    
                        <asp:Label ID="lblTotal" runat="Server"  />
                        
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
 
  <asp:GridView ID="GridView2" runat="server"

AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound" 

            DataSourceID="AccessDataSource1" Visible="true" >
            <Columns>
            <asp:TemplateField HeaderText="Devices And Services" Visible="True">
                    <ItemTemplate>
                        <asp:Label ID="lblItemDescription" runat="server"

Text='<%# Eval("itemDescription") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Rate">
                    <ItemTemplate>
                        <asp:Label ID="lblRate" runat="server" Text='<%# Eval("goldRate") %>' ></asp:Label>
                        
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Number">
                    <ItemTemplate>
                        <asp:TextBox ID="txtBoxNumber" Width="100px"

runat="Server"  />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Total">
                    <ItemTemplate>
                        <asp:Label ID="lblTotal" Text='<%# Eval("goldRateTotal") %>'  runat="Server" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        
        <asp:GridView ID="GridView3" runat="server"

AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound" 

            DataSourceID="AccessDataSource1" >
            <Columns>
            <asp:TemplateField HeaderText="Devices And Services" Visible="True">
                    <ItemTemplate>
                        <asp:Label ID="lblItemDescription" runat="server"

Text='<%# Eval("itemDescription") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Rate">
                    <ItemTemplate>
                        <asp:Label ID="lblRate" runat="server" Text='<%# Eval("silverRate") %>' ></asp:Label>
                        
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Number">
                    <ItemTemplate>
                        <asp:TextBox ID="txtBoxNumber" Width="100px"

runat="Server"   />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Total">
                    <ItemTemplate>
                        <asp:Label ID="lblTotal" Text='<%# Eval("silverRateTotal") %>'  runat="Server" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>        
    </div>
</form>
</body>
</html>

推荐答案

我宁愿看到一个像这样的GridView:
I would much rather see a single GridView like this:
<asp:gridview id="GridView2" runat="server" autogeneratecolumns="False"

   OnRowDataBound="GridView1_RowDataBound" 

   DataSourceID="AccessDataSource1" Visible="true" >
   <columns>
      <asp:templatefield headertext="Devices And Services" visible="True">
         <itemtemplate>
            <%# Eval("itemDescription") %>
         </itemtemplate>
      </asp:templatefield>
      <asp:templatefield headertext="Rate">
         <itemtemplate>
            <asp:label id="lblRate" runat="server"></asp:label>
         </itemtemplate>
      </asp:templatefield>
      <asp:templatefield headertext="Number">
         <itemtemplate>
            <asp:textbox id="txtBoxNumber" width="100px" runat="Server" />
         </itemtemplate>
      </asp:templatefield>
      <asp:templatefield headertext="Total">
         <itemtemplate>
            <asp:label id="lblTotal" runat="Server" />
         </itemtemplate>
      </asp:templatefield>
   </columns>
</asp:gridview>


And I would make your code behind something like this:


And I would make your code behind something like this:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
   if (e.Row.RowType == DataControlRowType.DataRow)
   {
      DataRow itemData = (DataRow) e.Row.DataItem;
      switch( DropDownList1.SelectedValue )
      {
         case "Platinum":				
            lblRate.Text = itemData["platinumRate"].ToString();
            break;
         case "Gold":
            lblRate.Text = itemData["goldRate"].ToString();
            break;
         case "Silver":
            lblRate.Text = itemData["silverRate"].ToString();
            break;
         default:
            lblRate.Text = 0.0; // Put your default value here
            break;
      }
      
	  TextBox txtBoxNumber = (TextBox)e.Row.FindControl("txtBoxNumber");				
      txtBoxNumber.Attributes.Add("onchange", "multiply(" + PR + ", '" + txtBoxNumber.ClientID + "','" + lblTotal.ClientID +  "')");
   }                                  
}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{         
   GridView1.DataBind();
}



The idea behind this is to make your UI much cleaner and simpler. Logic that defines how the grid is displayed is now contained within the RowDataBound handler which also places that logic in a more appropriate location.
The one thing I haven''t put in this code is object validation. You should always validate that objects like the "itemData" object and the "lblRate" and "txtBoxNumber" objects are not null before you try to use them. Never assume that they will be initialized properly.



The idea behind this is to make your UI much cleaner and simpler. Logic that defines how the grid is displayed is now contained within the RowDataBound handler which also places that logic in a more appropriate location.
The one thing I haven''t put in this code is object validation. You should always validate that objects like the "itemData" object and the "lblRate" and "txtBoxNumber" objects are not null before you try to use them. Never assume that they will be initialized properly.


这篇关于动态Gridview,下拉选择.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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