By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,865 Members | 1,717 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,865 IT Pros & Developers. It's quick & easy.

C#-WEB: Selected Value in User Control

Eleven
P: 19
Hi everybody!

I've got a User Control "SelectBatchByTerminal", that has two dropdown lists, "TerminalDropDownList" and "BatchDropDownList".

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11.  
  12. public partial class UserControls_SelectBatchByTerminal : System.Web.UI.UserControl
  13. {
  14.     public delegate void BatchChangedDelegate(Int32 Batch);
  15.     public event BatchChangedDelegate OnBatchChanged;
  16.  
  17.     public Int32 Batch
  18.     {
  19.         get
  20.         {
  21.             if (BatchDropDownList.Items.Count == 0)
  22.                 return 0;
  23.  
  24.             if (BatchDropDownList.SelectedValue == null)
  25.                 return 0;
  26.  
  27.             if (BatchDropDownList.SelectedIndex == 0)
  28.                 return 0;
  29.  
  30.             return Convert.ToInt32(BatchDropDownList.SelectedValue);
  31.         }
  32.     }
  33.  
  34.     public string SelectedTerminalID
  35.     {
  36.         get
  37.         {
  38.             if ((TerminalDropDownList.Items.Count == 0) || (TerminalDropDownList.SelectedIndex == 0))
  39.                 return "";
  40.  
  41.             return TerminalDropDownList.SelectedValue;
  42.         }
  43.     }
  44.  
  45.     public string SelectedBatch
  46.     {
  47.         get
  48.         {
  49.             if ((BatchDropDownList.Items.Count == 0) || (BatchDropDownList.SelectedIndex == 0))
  50.                 return "";
  51.  
  52.             return BatchDropDownList.SelectedValue;
  53.         }
  54.     }
  55.  
  56.     protected void Page_Load(object sender, EventArgs e)
  57.     {
  58.         if (!IsPostBack)
  59.             BindTerminals();
  60.     }
  61.  
  62.     protected void BindTerminals()
  63.     {
  64.         Trace.Write("Binding Data");
  65.         TerminalDropDownList.DataTextField = DAL.CashTrack.Terminals.Columns.TerminalId;
  66.         TerminalDropDownList.DataValueField = DAL.CashTrack.Terminals.Columns.TerminalId;
  67.         //Line below to be re-written to use SelectedTerminalId instead of LoggedInUser
  68.         TerminalDropDownList.DataSource = DAL.CashTrack.Sprocs.SpGetTerminalForBatch(Global.LoggedInUser.FkBanksID.Value).GetDataSet();
  69.         TerminalDropDownList.DataBind();
  70.         TerminalDropDownList.Items.Insert(0, new ListItem(">> Select <<", "0"));
  71.         TerminalDropDownList.SelectedIndex = 0;
  72.     }
  73.  
  74.     protected void BindBatches(string terminal)
  75.     {
  76.         BatchDropDownList.DataTextField = DAL.CashTrack.Deposits.Columns.Batch;
  77.         BatchDropDownList.DataValueField = DAL.CashTrack.Deposits.Columns.Batch;
  78.         BatchDropDownList.DataSource = DAL.CashTrack.Sprocs.SpGetBatchByTerminal(terminal).GetDataSet();
  79.         BatchDropDownList.DataBind();
  80.         BatchDropDownList.Items.Insert(0, new ListItem(">> Select <<", "0"));
  81.         BatchDropDownList.SelectedIndex = 0;
  82.     }
  83.  
  84.     protected void TerminalDropDownList_SelectedIndexChanged(object sender, EventArgs e)
  85.     {
  86.         BindBatches(TerminalDropDownList.SelectedValue);
  87.     }
  88.  
  89.     protected void BatchDropDownList_SelectedIndexChanged(object sender, EventArgs e)
  90.     {
  91.         if (this.Batch != 0)
  92.         {
  93.             if (OnBatchChanged != null)
  94.                 OnBatchChanged(this.Batch);
  95.         }
  96.     }
  97. }
  98.  
And I've got a page "DepositPerBatch" that uses that UC..
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11.  
  12. public partial class Deposits_DepositPerBatch : System.Web.UI.Page
  13. {
  14.     protected override void OnInit(EventArgs e)
  15.     {
  16.         base.OnInit(e);
  17.  
  18.         wiz.ActiveStepIndex = 0;
  19.         SelectBatchByTerminalUC.OnBatchChanged += new UserControls_SelectBatchByTerminal.BatchChangedDelegate(OnBatchChanged);
  20.     }
  21.  
  22.     protected void Page_Load(object sender, EventArgs e)
  23.     {
  24.  
  25.     }
  26.  
  27.     protected void OnBatchChanged(Int32 Batch)
  28.     {
  29.  
  30.     }
  31.  
  32.     protected void wiz_NextButtonClick(object sender, WizardNavigationEventArgs e)
  33.     {
  34.         try
  35.         {
  36.             if (SelectBatchByTerminalUC.Batch == 0)
  37.                 throw new Exception("Please select a Batch Number to continue");
  38.  
  39.             // Now we can query the data for the deposit
  40.             DAL.CashTrack.VwDepositsByBatchCollection deposit = new DAL.CashTrack.VwDepositsByBatchCollection().Where(DAL.CashTrack.VwDepositsByBatch.Columns.Batch, SelectBatchByTerminalUC.Batch).OrderByDesc(DAL.CashTrack.VwDeposits.Columns.AccountNumber).Load();
  41.             if (deposit.Count == 0)
  42.                 throw new Exception("Sorry... We are unable to locate any deposits for the selected batch");
  43.  
  44.             BatchNumberLabel.Text = Convert.ToString(deposit[0].Batch);
  45.             TerminalLabel.Text = "TBD"; //deposit[0].TerminalId;
  46.  
  47.             Grid1.DataSource = DAL.CashTrack.VwDepositsByBatch.FetchByParameter(DAL.CashTrack.VwDepositsByBatch.Columns.Batch, SelectBatchByTerminalUC.Batch, SubSonic.OrderBy.Asc(DAL.CashTrack.VwDepositsByBatch.Columns.DateX));
  48.             Grid1.DataBind();
  49.         }
  50.         catch (Exception ex)
  51.         {
  52.             e.Cancel = true;
  53.             Savitar.Web.Utils.Popup.ShowDialog(this, ex.Message);
  54.         }
  55.     }
  56. }
  57.  

The page works but it displays all the terminals, I want to change the following line to use the terminal selected in the user control, instead of the LoggedInUser

Expand|Select|Wrap|Line Numbers
  1. TerminalDropDownList.DataSource = DAL.CashTrack.Sprocs.SpGetTerminalForBatch(Global.LoggedInUser.FkBanksID.Value).GetDataSet();
  2.  
I know I have to change my Stored Procedure as well, which currently looks like this:
Expand|Select|Wrap|Line Numbers
  1. ALTER PROCEDURE [dbo].[spGetTerminalForBatch]
  2. (
  3.   @BankID INT
  4. )
  5. AS
  6.  
  7. SELECT DISTINCT TerminalId
  8. FROM vwDepositsByBatch
  9. WHERE fkbankId = @BankID AND TerminalID <> '0'
  10. ORDER BY TerminalId
  11.  
The TerminalId is a string!

Any help would be appreaciated, let me know if you don't understand what I wrote, i'll try and explain it a bit more.
Mar 11 '08 #1
Share this Question
Share on Google+
1 Reply


Eleven
P: 19
Expand|Select|Wrap|Line Numbers
  1. ALTER PROCEDURE [dbo].[spGetTerminalForBatch]
  2. (
  3.   @Terminal VARCHAR(8)
  4. )
  5. AS
  6.  
  7.  
  8. SELECT DISTINCT TerminalId
  9. FROM vwDepositsByBatch
  10. WHERE TerminalId = @Terminal
  11. ORDER BY TerminalId
  12.  
I wrote a new SP but I'm not really sure what to edit on my C# code
Mar 11 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.