动态控制asp net中的求和值

时间:2014-02-01 16:18:15

标签: c# asp.net visual-studio

我创建了一个动态控制脚本。我想要做的是在“.aspx.cs文件”中的“LabelTotal”的单个标签中对“Details.ascx”中的所有“LabelVlera”求和。

我希望你能理解我。谢谢。

在UserControl文件夹上我有这个脚本......

Details.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ContactDetails.ascx.cs" Inherits="UserControl_ContactDetails" EnableViewState="false"  %>
    <style type="text/css">
        .auto-style1 {
            width: 100%;
        }
        .auto-style2 {
            width: 150px;
        }
        .auto-style3 {
            width: 294px;
        }
        .auto-style4 {
            width: 150px;
            text-align: right;
        }
    </style>
<table class="auto-style1">
    <tr>
        <td class="auto-style4">
            <asp:Label ID="Label1" runat="server" Text="Artikulli:"></asp:Label>
        </td>

        <td class="auto-style3">
            <asp:DropDownList ID="DropDownListArtikulli" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownListArtikulli_SelectedIndexChanged">
            </asp:DropDownList>
        </td>
        <td>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="DropDownListArtikulli" ErrorMessage="Zgjidhni artikullin"></asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr>
        <td class="auto-style4">
            <asp:Label ID="Label2" runat="server" Text="Cmimi:"></asp:Label>
        </td>
        <td class="auto-style3">
            <asp:Label ID="LabelCmimi" runat="server"></asp:Label>
            <asp:Label ID="Label3" runat="server" Text="lek"></asp:Label>
        </td>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td class="auto-style4">
            <asp:Label ID="Label4" runat="server" Text="Sasia:"></asp:Label>
        </td>
        <td class="auto-style3">
            <asp:TextBox ID="TextBoxSasia" runat="server" AutoPostBack="True"></asp:TextBox>
        </td>
        <td>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="TextBoxSasia" ErrorMessage="Vendosni sasine"></asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr>
        <td class="auto-style4">
            <asp:Label ID="Label5" runat="server" Text="Vlera:"></asp:Label>
        </td>
        <td class="auto-style3">
            <asp:Label ID="LabelVlera" runat="server" ></asp:Label>
            <asp:Label ID="Label6" runat="server" Text="lek"></asp:Label>
        </td>
        <td>&nbsp;</td>
    </tr>
    </table>

<hr />

Details.ascx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data.SqlClient;
using System.Configuration;
using System.Data;

public partial class UserControl_ContactDetails : System.Web.UI.UserControl
{

    #region Public Properties

    public Label VlTotal
    {
        get
        {
            return LabelVlera;
        }
        set
        {
            LabelVlera = value;
        }
    }

    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        if(Page.IsPostBack)
        {
            DropDownListArtikulli.Text = Request.Form[DropDownListArtikulli.UniqueID];
            LabelCmimi.Text = Request.Form[LabelCmimi.UniqueID];
            TextBoxSasia.Text = Request.Form[TextBoxSasia.UniqueID];
            LabelVlera.Text = Request.Form[LabelVlera.UniqueID];
        }
        Cmimi();
        Vlera();
    }

    private void Cmimi()
    {
        DataTable listaArtikujt = new DataTable();

        using (SqlConnection lidhje = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString))
        {
            try
            {

                SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM [Artikujt]", lidhje);
                adapter.Fill(listaArtikujt);

                DropDownListArtikulli.DataSource = listaArtikujt;
                DropDownListArtikulli.DataTextField = "Artikulli";
                DropDownListArtikulli.DataValueField = "Cmimi";
                DropDownListArtikulli.DataBind();

               LabelCmimi.Text = DropDownListArtikulli.SelectedValue.ToString();

            }
            catch (Exception ex)
            {
                Response.Write("Gabim:" + ex.ToString());
            }
        }
    }

    protected void DropDownListArtikulli_SelectedIndexChanged(object sender, EventArgs e)
    {
        Cmimi();
    }

    private void Vlera()
    {
        if(!string.IsNullOrEmpty(TextBoxSasia.Text))
        {
            LabelVlera.Text = TextBoxSasia.Text.ToString();

            int a = Convert.ToInt32(LabelCmimi.Text);
            int b = Convert.ToInt32(LabelVlera.Text);

            int c = a * b;
            LabelVlera.Text = c.ToString();
        }
    }

    protected void TextBoxSasia_TextChanged(object sender, EventArgs e)
    {
        Vlera();
    }
}

0 个答案:

没有答案