Can anyone PLEASE help me understand this?

evilpker

New Member
On the submit, the userdepartment dropdown selected index always equals -1 I have tried alot of things but I can't get it to work. <BR><BR>See code Below.<BR><BR>Dropdowns have been giving me more trouble then I ever thought that they would. Anyone help me out with some common mistakes that I might be making time and time again? If I can't get it work, would you suggest that I get it's clientID and just try to get it's value from the forms collection? <BR><BR><BR>I'm going nuts over here. <BR><BR>Thanks. <BR><BR>ASPX Page<BR><%@ Control Language="c#" AutoEventWireup="false" Codebehind="UserAdmin.ascx.cs" Inherits="Admin.UserAdmin" %><BR><form runat="server"><BR> <div class="moduleHeading"><BR> <asp:Label enableviewstate="false" id="ModuleName" runat="server" /></div><BR> <div id="UserSelection" runat="server" visible="false"><BR> <b>Current Users</b><BR> <BR><BR> <asp:DataGrid id="UserList" runat="server" autogeneratecolumns="false" bordercolor="#000000" borderwidth="1" cellspacing="0" cellpadding="3"><BR> <alternatingitemstyle cssclass="evenRow" /><BR> <headerstyle cssclass="reportHeading" /><BR> <itemstyle cssclass="oddRow" /><BR> <columns><BR> <asp:templatecolumn><BR> <itemtemplate><BR> <a href=http://aspmessageboard.com/archive/index.php/'<%# GetNavUrl((int)DataBinder.Eval(Container.DataItem, "userId")) %>'>edit</a><BR> </itemtemplate><BR> </asp:templatecolumn><BR> <asp:boundcolumn datafield="name" headertext="Name" /><BR> <asp:boundcolumn datafield="username" headertext="Username" /><BR> </columns><BR> </asp:DataGrid><BR> </div><BR> <div id="UserOptions" runat="server" visible="true" enableviewstate="True"><BR> <BR><BR> <table><BR> <tr><BR> <td align="middle" colspan="2"><BR> <strong>User Name :</strong> <asp:label id="UserName" runat="server" /><BR> <BR><BR><BR> </td><BR> </tr><BR> <tr><BR> <td><BR> <strong>New Password :</strong><BR> <BR><asp:TextBox id="UserPassword" runat="server" TextMode="Password" /><asp:RequiredFieldValidator id="UserPasswordValidator" runat="server" ErrorMessage="Password Required" ControlToValidate="UserPassword" Display="None" /><asp:RegularExpressionValidator id="UserPasswordExpressionValidator" runat="server" ErrorMessage="Password Does Not Meet Minimum Requirements (6 - 12 characters long)" ControlToValidate="UserPassword" Display="None" ValidationExpression="w{6,12}" /><BR> </td><BR> <td><BR> <strong>Confirm Password :</strong><BR> <BR><asp:textbox id="UserPasswordConfirm" runat="server" textmode="Password" /><asp:requiredfieldvalidator id="UserPasswordConfirmValidator" runat="server" errormessage="Confirm Password Required" controltovalidate="UserPasswordConfirm" display="None" /><asp:comparevalidator id="UserPasswordConfirmCompareValidator" runat="server" ErrorMessage="Password And Confirm Password Do Not Match" ControlToCompare="UserPassword" ControlToValidate="UserPasswordConfirm" Display="None" /><BR> </td><BR> </tr><BR> <tr><BR> <td><BR> <strong>First Name :</strong><BR> <BR><asp:TextBox id="UserFirstName" runat="server" /><asp:RequiredFieldValidator id="UserFirstNameValidator" runat="server" ErrorMessage="First Name Required" ControlToValidate="UserFirstName" Display="None" /></td><BR> <td><BR> <strong>Last Name :</strong><BR> <BR><asp:TextBox id="UserLastName" runat="server" /><asp:RequiredFieldValidator id="UserLastNameValidator" runat="server" ErrorMessage="Last Name Required" ControlToValidate="UserLastName" Display="None" /></td><BR> </tr><BR> <tr><BR> <td><BR> <strong>Email :</strong><BR> <BR><asp:TextBox id="UserEmail" runat="server" /><asp:RequiredFieldValidator id="UserEmailValidator" runat="server" ErrorMessage="Email Required" ControlToValidate="UserEmail" Display="None" /><asp:RegularExpressionValidator id="UserEmailExpressionValidator" runat="server" ErrorMessage="Invalid Email Address" ControlToValidate="UserEmail" Display="None" ValidationExpression="w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*" /></td><BR> <td><BR> <strong>Phone :</strong><BR> <BR><asp:TextBox id="UserAreaCode" runat="server" Width="40px" MaxLength="3" /> <asp:TextBox id="UserPhone" runat="server" Width="110px" MaxLength="8" /><asp:RequiredFieldValidator id="UserAreaCodeValidator" runat="server" ErrorMessage="Area Code Required" ControlToValidate="UserAreaCode" Display="None" /><asp:RegularExpressionValidator id="UserAreaCodeExpressionValidator" runat="server" ErrorMessage="Invalid Areacode" ControlToValidate="UserAreaCode" Display="None" ValidationExpression="d{3}" /><asp:RequiredFieldValidator id="UserPhoneValidator" runat="server" ErrorMessage="Phone Number Required" ControlToValidate="UserPhone" Display="None" /><asp:RegularExpressionValidator id="UserPhoneExpressionValidator" runat="server" ErrorMessage="Invalid Phone Number (please use this format 123-1234)" ControlToValidate="UserPhone" Display="None" ValidationExpression="d{3}-d{4}" /></td><BR> </tr><BR> <tr><BR> <td colspan="2"><BR> <strong>Default Department :</strong><BR> <BR><asp:DropDownList id="UserDepartmentDrop" runat="server" enableviewstate="True" datatextfield="name" datavaluefield="id" /></td><BR> </tr><BR> <tr><BR> <td align="middle" colspan="2"><BR> <asp:Button id="UpdateUser" runat="server" Text="Save Changes" /></td><BR> </tr><BR> </table><BR> </div><asp:ValidationSummary id="ValidationSummary1" runat="server" CssClass="error" ForeColor=" " HeaderText="Error:" /><BR></form><BR><BR>CS Source<BR>namespace Admin<BR>{<BR> using System;<BR> using System.Collections.Specialized;<BR> using System.Configuration;<BR> using System.Data;<BR> using System.Data.SqlClient;<BR> using System.Drawing;<BR> using System.Text;<BR> using System.Web;<BR> using System.Web.UI;<BR> using System.Web.UI.WebControls;<BR> using System.Web.UI.HtmlControls;<BR><BR> /// <summary><BR> /// Summary description for UserAdmin.<BR> /// </summary><BR> public abstract class UserAdmin : System.Web.UI.UserControl<BR> {<BR> #region Variable Declarations<BR> #region Web Controls<BR> protected System.Web.UI.HtmlControls.HtmlGenericControl UserOptions;<BR> protected System.Web.UI.HtmlControls.HtmlGenericControl UserSelection;<BR> protected System.Web.UI.WebControls.DataGrid UserList;<BR> protected System.Web.UI.WebControls.Button UpdateUser;<BR> protected System.Web.UI.WebControls.DropDownList UserDepartmentDrop;<BR> protected System.Web.UI.WebControls.Label ModuleName;<BR> protected System.Web.UI.WebControls.Label UserName;<BR> protected System.Web.UI.WebControls.TextBox UserFirstName;<BR> protected System.Web.UI.WebControls.TextBox UserLastName;<BR> protected System.Web.UI.WebControls.TextBox UserEmail;<BR> protected System.Web.UI.WebControls.TextBox UserAreaCode;<BR> protected System.Web.UI.WebControls.TextBox UserPhone;<BR> protected System.Web.UI.WebControls.TextBox UserPassword;<BR> protected System.Web.UI.WebControls.TextBox UserPasswordConfirm;<BR> protected System.Web.UI.WebControls.RequiredFieldValidator UserFirstNameValidator;<BR> protected System.Web.UI.WebControls.RequiredFieldValidator UserEmailValidator;<BR> protected System.Web.UI.WebControls.RequiredFieldValidator UserLastNameValidator;<BR> protected System.Web.UI.WebControls.RequiredFieldValidator UserAreaCodeValidator;<BR> protected System.Web.UI.WebControls.RequiredFieldValidator UserPasswordValidator;<BR> protected System.Web.UI.WebControls.RequiredFieldValidator UserPasswordConfirmValidator;<BR> protected System.Web.UI.WebControls.RequiredFieldValidator UserPhoneValidator;<BR> protected System.Web.UI.WebControls.RegularExpressionValidat or UserEmailExpressionValidator;<BR> protected System.Web.UI.WebControls.RegularExpressionValidat or UserAreaCodeExpressionValidator;<BR> protected System.Web.UI.WebControls.RegularExpressionValidat or UserPhoneExpressionValidator;<BR> protected System.Web.UI.WebControls.RegularExpressionValidat or UserPasswordExpressionValidator;<BR> protected System.Web.UI.WebControls.CompareValidator UserPasswordConfirmCompareValidator;<BR> protected System.Web.UI.WebControls.ValidationSummary ValidationSummary1;<BR> #endregion<BR><BR> private DataView sectionModules;<BR> private int userId = -1;<BR> private SqlConnection myConnection;<BR> #endregion<BR><BR> private void Page_Load(object sender, System.EventArgs e)<BR> {<BR> // Put user code to initialize the page here<BR> myConnection = new SqlConnection((String) ((NameValueCollection) ConfigurationSettings.GetConfig("system.web/dbConnStore"))["CashAppDB"]);<BR><BR> if(Request.QueryString["userId"] != null)<BR> userId = int.Parse(Request.QueryString["userId"]);<BR><BR> if(userId == -1)<BR> {<BR> UserSelection.Visible = true;<BR> BindUserList();<BR> }<BR> else<BR> {<BR> if(!IsPostBack)<BR> ShowUserData();<BR> }<BR> Trace.Warn("UserDepartmentDrop", UserDepartmentDrop.SelectedIndex.ToString());<BR> }<BR><BR> private void ShowUserData()<BR> {<BR> DataSet userAdminData = new DataSet();<BR> DataView departments;<BR> DataView userData;<BR> SqlCommand myCommand = new SqlCommand("spUserAdmin", myConnection);<BR> SqlDataAdapter myAdapter;<BR><BR> UserOptions.Visible = true;<BR><BR> myCommand.CommandType = CommandType.StoredProcedure;<BR> myCommand.Parameters.Add("@userId", SqlDbType.SmallInt).Value = userId;<BR><BR> myAdapter = new SqlDataAdapter(myCommand);<BR> myAdapter.Fill(userAdminData);<BR> myConnection.Close();<BR><BR> departments = userAdminData.Tables[1].DefaultView;<BR> userData = userAdminData.Tables[0].DefaultView;<BR><BR> UserDepartmentDrop.DataSource = departments;<BR> UserDepartmentDrop.DataBind();<BR> UserDepartmentDrop.Items.FindByValue(userData[0]["departmentId"].ToString()).Selected = true;<BR><BR> UserName.Text = userData[0]["username"].ToString();<BR> UserFirstName.Text = userData[0]["firstname"].ToString();<BR> UserLastName.Text = userData[0]["lastname"].ToString();<BR> UserEmail.Text = userData[0]["email"].ToString();<BR> UserAreaCode.Text = userData[0]["areacode"].ToString();<BR> UserPhone.Text = userData[0]["phone"].ToString();<BR> }<BR><BR><BR> private void BindUserList()<BR> {<BR> SqlCommand myCommand = new SqlCommand("spUsersGet", myConnection);<BR> SqlDataReader myReader;<BR><BR> myCommand.CommandType = CommandType.StoredProcedure;<BR><BR> myConnection.Open();<BR> myReader = myCommand.ExecuteReader();<BR><BR> UserList.DataSource = myReader;<BR> UserList.DataBind();<BR><BR> myReader.Close();<BR> myConnection.Close();<BR> }<BR><BR> protected string GetNavUrl(int userId)<BR> {<BR> return Request.Path + "?" + Request.QueryString + "&userId=" + userId.ToString();<BR> }<BR><BR> #region Web Form Designer generated code<BR> override protected void OnInit(EventArgs e)<BR> {<BR> //<BR> // CODEGEN: This call is required by the ASP.NET Web Form Designer.<BR> //<BR> InitializeComponent();<BR> base.OnInit(e);<BR> }<BR> <BR> /// Required method for Designer support - do not modify<BR> /// the contents of this method with the code editor.<BR> /// </summary><BR> private void InitializeComponent()<BR> {<BR> this.UpdateUser.Click += new System.EventHandler(this.UpdateUser_Click);<BR> this.Load += new System.EventHandler(this.Page_Load);<BR><BR> }<BR> #endregion<BR><BR> private void UpdateUser_Click(object sender, System.EventArgs e)<BR> {<BR> Trace.Warn("UserDepartmentDrop", UserDepartmentDrop.SelectedIndex.ToString());<BR> if(Page.IsValid)<BR> {<BR> Response.Write("<div>UpdateUserClick");<BR> Response.Write("<BR>New Password : " + UserPassword.Text);<BR> Response.Write("<BR>First Name : " + UserFirstName.Text);<BR> Response.Write("<BR>Last Name : " + UserLastName.Text);<BR> Response.Write("<BR>Email : " + UserEmail.Text);<BR> Response.Write("<BR>AreaCode : " + UserAreaCode.Text);<BR> Response.Write("<BR>Phone : " + UserPhone.Text);<BR> Response.Write("<BR>Default Department : " + UserDepartmentDrop.SelectedIndex);<BR> Response.Write("</div>");<BR> }<BR> }<BR> }<BR>}I didnt try to decifer your code, but look at the following example and see if it helps.<BR><BR><%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="Delete1.WebForm1" %><BR><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" ><BR><HTML><BR> <HEAD><BR> <title>WebForm1</title><BR> <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0"><BR> <meta name="CODE_LANGUAGE" Content="C#"><BR> <meta name="vs_defaultClientScript" content="JavaScript"><BR> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"><BR> </HEAD><BR> <body MS_POSITIONING="GridLayout"><BR> <form id="Form1" method="post" runat="server"><BR> <asp:DropDownList id="lst1" style="Z-INDEX: 102; LEFT: 77px; POSITION: absolute; TOP: 195px" runat="server" Width="164px"></asp:DropDownList><BR> <asp:Button id="Button1" style="Z-INDEX: 103; LEFT: 184px; POSITION: absolute; TOP: 240px" runat="server" Text="Button"></asp:Button><BR> </form><BR> </body><BR></HTML><BR><BR><BR>using System;<BR>using System.Collections;<BR>using System.ComponentModel;<BR>using System.Data;<BR>using System.Drawing;<BR>using System.Web;<BR>using System.Web.SessionState;<BR>using System.Web.UI;<BR>using System.Web.UI.WebControls;<BR>using System.Web.UI.HtmlControls;<BR><BR>namespace Delete1<BR>{<BR> /// <summary><BR> /// Summary description for WebForm1.<BR> /// </summary><BR> public class WebForm1 : System.Web.UI.Page<BR> {<BR> protected System.Web.UI.WebControls.DropDownList lst1;<BR> protected System.Web.UI.WebControls.Button Button1;<BR> protected System.Web.UI.WebControls.DataGrid DataGrid1;<BR> <BR> private void Page_Load(object sender, System.EventArgs e)<BR> {<BR> if(!Page.IsPostBack)<BR> {<BR> lst1.Items.Add("One");<BR> lst1.Items.Add("Two");<BR> lst1.Items.Add("Three");<BR> }<BR> }<BR><BR><BR> #region Web Form Designer generated code<BR> override protected void OnInit(EventArgs e)<BR> {<BR> //<BR> // CODEGEN: This call is required by the ASP.NET Web Form Designer.<BR> //<BR> InitializeComponent();<BR> base.OnInit(e);<BR> }<BR> <BR> /// <summary><BR> /// Required method for Designer support - do not modify<BR> /// the contents of this method with the code editor.<BR> /// </summary><BR> private void InitializeComponent()<BR> { <BR> this.Button1.Click += new System.EventHandler(this.Button1_Click);<BR> this.Load += new System.EventHandler(this.Page_Load);<BR> }<BR> #endregion<BR><BR><BR> private void Button1_Click(object sender, System.EventArgs e)<BR> {<BR> int index = lst1.SelectedIndex;<BR> }<BR> }<BR><BR>}<BR>
 
Back
Top