MVC :: Listboxfor And MultiSelectLists / How To Get The providers's Categories To Be Selected On The Load Of The Page
Nov 19, 2010
I am a student at Millsaps College in Jackson, Mississippi and im working on my Senior Project. For my project i have chosen to use ASP.net MVC 2. Most things seem to be going well, but im having a real issue with the ListBoxFor() method. I have read a ton of posts on this website, StackOverflow as well as others. Here is a description of my problem.
One of the major points of my project is to create a directory of healthcare service providers in Mississippi. These providers can be sorted into multple categories, which creates a many-to-many relationship. Im using a ListBoxFor() to display those categories
when the user would like to edit or create a provider. In the case of creation things work well. However in the case of editing an existing provider, i am unable to figure out how to get the providers's categories to be selected on the load of the page. Below is a look at some of the code. I would be so thankful to anyone who could help me tackle this issue
List<Category> Category is my database table having two properties Category_Id and Category_Name
and a strongly typed ProductCategoryViewModel View in which a ListBoxFor HtmlHelper is filled with Category_Name and Category_Id
public ActionResult Index(ProductCategoryViewModel model) { if (ModelState.IsValid)[code]....
In this Article is also my Database table defined in Linq to Sql classes. I want to get selected value and text from HtmlHelper ListBox to insert it to my Article table
i posted a question yesterday but it got taken off by moderator mbanavige with the reason indicating it was a duplicate post. i know for sure it was the first time i was asking that question. i will have to ask it again NOW because it was deleted. hope THIS one does not duplicate the one i made yesterday. so here goes:
I need to query a products table based on categories selected by the user. The selection of the categories is a checkbox list, and user can select any number of categories (including none at all). I only know how to compare one parameter at a time. Like this:
I want to add unlimited categories and sub categories facility, But I couldn't solve it for last two days.
1. DB
CategID,CategName,parentid
Data:
CategID CategName Parent ID 1 Electronics 0 (where 0 means it is main category) 2 TV 1 (Self Join whith the categ id Electronics) 3 Panasonic 2 4 LG 2 5 Toys 0 6 Girls Toys 5 7 Boys Toys 5 8 Criket Related 7 9 Dolls 6
Here I want to display this data in Combo Box like
In my web form, i have 2 drop downlist controls. eg. dropdown1 and dropdown2. I will change the dropdownlist1 item, consequently selectedindexchanged event fires and reloads the dropdownlist2 items. And i have another button also in order to get the result based on the dropdown1 and dropdown2. Lets say for example, i have a list of countries in dropdown1 and a list of states in dropdown2. I selected "India" in dropdown1 and correspondingly "Tamilnadu" in dropdown2. And then click a button which calls the dataset based on dropdown1 and dropdown2. The problem i face here is, whenever i click on the button to load the dataset, dropdown1 value remains the same as the dataset is not called again, whereas the dropdown2 value changes to the first one as it gets called automatically during postback. For your information, I have loaded both the lists in the page load using (!IsPostback) property.
I posted a similar question previously, but none of the answers worked and I've been scouring all over the web trying to find a solution. My situation, I have a Edit Window webform with a dropdownlist (Note: to avoid confusion, I'm using Telerik extensions only to decorate the webform):
And in the code behind, I'm setting the datasource for the dropdownlist and using a function to query the DB for the name of the value I want to set as the selected value when the page loads initially: Partial Class EditFormVB Inherits System.Web.UI.Page
Public Shared category_Name As String = "" Dim ddlDataSource As New SqlDataSource Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init DetailsView1.DefaultMode = DetailsViewMode.Edit End Sub Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Me.Page.Title = "Editing record" ''Setup DropDownList SqlDataSource ddlDataSource.ID = "ReqCategoryData" Page.Controls.Add(ddlDataSource) ddlDataSource.ConnectionString = ConfigurationManager.ConnectionStrings("TTPRODReportsQuery").ConnectionString ddlDataSource.SelectCommand = "SELECT TS_NAME AS ReqCategory FROM dbo.TS_SELECTIONS WHERE (TS_FLDID = 5299 AND TS_STATUS = 0) ORDER BY TS_NAME" Dim args As New DataSourceSelectArguments ddlDataSource.Select(args) ''Set max length of Title field to 70 characters Dim dvrTest As DetailsViewRowCollection = DetailsView1.Rows Dim TitleTB As TextBox = dvrTest.Item(0).Cells(1).Controls(0) TitleTB.Attributes.Add("onkeydown", "isMaxLen(this)") TitleTB.Attributes.Add("maxlength", "70") Dim myDDL As DropDownList = DetailsView1.FindControl("reqCategoryDropDown") ''Perform dropdown list population operations If Page.IsPostBack = False Then Dim ticket_ID As String = getDataKey(DetailsView1) ''Fetch Category ID Dim sqlText As String = "SELECT TS_REQCATEGORY FROM USR_ITFAC WHERE (TS_ID = " + ticket_ID + ") " Dim reqDataReader As SqlDataReader = GetDataReader(sqlText) reqDataReader.Read() Dim category_ID As String = reqDataReader(0) ''Fetch Category name using the categoryID and set as selected value in dropdown list sqlText = "SELECT TS_NAME FROM TS_SELECTIONS WHERE (TS_ID = " + category_ID + ") " reqDataReader = GetDataReader(sqlText) reqDataReader.Read() category_Name = reqDataReader(0) myDDL.DataBind() myDDL.Selectedvalue = category_Name //<--this value gets set only when debugging, End If End Sub Private Function GetDataReader(ByVal sqlText As String) As SqlDataReader Dim dr As SqlDataReader Dim sqlConn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("TTPRODReportsQuery").ConnectionString) sqlConn.Open() Dim sqlCmd As SqlCommand = New SqlCommand(sqlText, sqlConn) dr = sqlCmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection) Return dr End Function End Class
The dropdownlist does get populated appropriately, however when I attempt to set the value, it doesn't reflect when the page loads; the dropdownlist just gets populated and no value is set to selected in the markup, so the first value is shown by default. The odd thing is that when I'm debugging, the value appears to get set when I step through the function, it's as if the selectedvalue is getting reset as soon as the function exits and proceeds to load the page.
SOLUTION: Had to add a separate function that is called onLoad from the DropDownList after the after Page_Load is done executing. Still unresolved is why the the DropDownList rebinds after the Page_Load.
IN HTML: <asp:DropDownList DataSourceID="ReqCategoryData" DataTextField="ReqCategory" DataValueField="ReqCategory" ID="reqCategoryDropDown" runat="server" AutoPostBack="true" OnLoad="DDL_DataBound"> IN CODE-BEHIND Public Shared category_Name As String = "" Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Me.Page.Title = "Editing record" ''Setup DropDownList SqlDataSource ddlDataSource.ID = "ReqCategoryData" Page.Controls.Add(ddlDataSource) ddlDataSource.ConnectionString = ConfigurationManager.ConnectionStrings("TTPRODReportsQuery").ConnectionString ddlDataSource.SelectCommand = "SELECT TS_NAME AS ReqCategory FROM dbo.TS_SELECTIONS WHERE (TS_FLDID = 5299 AND TS_STATUS = 0) ORDER BY TS_NAME" Dim args As New DataSourceSelectArguments ddlDataSource.Select(args) ''Set max length of Title field to 70 characters Dim dvrTest As DetailsViewRowCollection = DetailsView1.Rows Dim TitleTB As TextBox = dvrTest.Item(0).Cells(1).Controls(0) TitleTB.Attributes.Add("onkeydown", "isMaxLen(this)") TitleTB.Attributes.Add("maxlength", "70") Dim myDDL As DropDownList = DetailsView1.FindControl("reqCategoryDropDown") ''Perform dropdown list population operations If Page.IsPostBack = False Then Dim ticket_ID As String = getDataKey(DetailsView1) ''Fetch Category ID Dim sqlText As String = "SELECT TS_REQCATEGORY FROM USR_ITFAC WHERE (TS_ID = " + ticket_ID + ") " Dim reqDataReader As SqlDataReader = GetDataReader(sqlText) reqDataReader.Read() Dim category_ID As String = reqDataReader(0) ''Fetch Category name using the categoryID and set as selected value in dropdown list sqlText = "SELECT TS_NAME FROM TS_SELECTIONS WHERE (TS_ID = " + category_ID + ") " reqDataReader = GetDataReader(sqlText) reqDataReader.Read() category_Name = reqDataReader(0) myDDL.DataBind() End If End Sub Protected Sub DDL_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) If Page.IsPostBack = False Then Dim myDDL As DropDownList = DetailsView1.FindControl("reqCategoryDropDown") myDDL.Items.FindByValue(category_Name).Selected = True End If End Sub
how to make a item the default from a dropdownlist, I want to make the value GBP as the option selected when the page loads, i've spent all day trying to get ddlist to work and now i'm stuck on this.
this
.ViewData["ddlCurrency"] = new SelectList(ws.ToList(), "dboCurrencyID", "dboCountry", new SelectListItem { Selected = true, Text = "United Kingdom (GBP)", Value = "GBP" });
IÂ Â have a drop down box , it has value that is retrieve from datasqlsource, however i want to get the count value on page load but it seem that it did not count
'Dim adapter As New SqlDataAdapter 'Dim ds As New DataSet 'Dim connectionString = ConfigurationManager.ConnectionStrings("myProject").ConnectionString 'Dim myConn As New SqlConnection(connectionString)
in this code i have generated runtime checkboxlist controls. my prob is in the page load i want to get the chekboxlist items selected for the items which the user has selected previously and saved. now i get only the last item in the loop as selected and the remaining are not selected.
I have a function on page load which will work based on dropdown selected value. and I am using this dropdown in a user control. So getting problem because the dropdown value is not loaded yet before Page Load. How Can I do that using those control still.
Here is my code below.
public partial class GradeEntry : System.Web.UI.Page { StudentManager studentManager = null; CourseManager courseManagerObj = null; GradeManager gradeManager = null;
Question basically crams it all in... I'm loading a page with a querystring (ID), and I need to use that ID to set the selected item of a ListView when the page loads. The ID is a DataKey on the ListView. I have no code of value to post--none of my attempts at this work.
i have agridview to show the records of tables, this grid view has button infront of each record , i want to press this button to display the selected record in new page to edit it and return it again to gridview
I am trying to put a list of categories on my shared master page, I have it working by passing my list of categories to the shared master page in a viewdata[] object - Problem I have is I now want to loop through and out put the links to my categories (Which I am doing this)
I hv Declared two Master page one Is Base and Derived.. Base Page Load is working but when i hv written load controls in Derived Page Load Using C# it's not working..
I have installed MySQL and set up an ODBC driver to point to a database in MySQL. In ASP.NET I have defined a connection string to attach to this MySQL dtabase.
However in ASP.NET connection wizard, although the tables are shown, no colums are shown and when I attempt to run the page which uses this datasource I get an error message about a syntax error near page 1 at 'Select * from [categories]'. how to use an ODBC (MySQL) datasource in ASP.NET?
I am trying to create a progress bar for page load as it takes long to load. I need help to resolve jscript error 'null' is null or not an object on line $get("btn").click();
I have a update panel on the ASPX page,When thepage loads the content in the update panel shouldnot load ( Update panel should show the Updatepanel progress control) but after page load update panel contents should load . How do i get this efect.
I have two gridviews on same page - If a record is selected in Gridview1, I need to unselect a record (if one is selected) in Gridview2 - and vice-versa.
we are creating a custom content management and out portal page is bit bulky it is about 60Kb without images.
and during loading the page in some browser we can see some parts of site load faster than the other parts of the site where as we want to indicate (or instruct the web server) to load some of the areas first then load rest of the page.
is there any particular setting in IIS for is there any particular method in classic asp for doing that? also I have the same question in asp.net.