asp.net help - Another Problem

SelArom

Senior member
Sep 28, 2004
872
0
0
www.djselarom.com
i got the other problem solved, but how do you name the primary key at runtime for a dataset? It's just coming out as Constraint1. I want to give it a specific name.

-SelArom
 

Mark R

Diamond Member
Oct 9, 1999
8,513
16
81
I'm not quite sure what you want.

Are you saying that you want a seperate sub-genre table for each item in the main genres table? That's what it looks like from your code.

Probably the easiest way to do it, is to bind the subcategories datalists at the time each one is created, by handling the ItemDataBound events fired from the main datalist. That's the way I'd do it anyway.

In the .aspx file you no longer need to specify the datasource and datamember properties. These will be specified seperately for each sub-datalist when the page runs.
Then in your code you need to add a handler for the ItemDataBound event

//Code follows
//This Line goes in page_init or page_load
dlLeftNav.ItemDataBound += new ItemDataBoundEventHandler (dlLeftNav_ItemDataBound);



protected void dlLeftNav_ItemDataBound (object sender, DataListItemEventArgs e)
{
DataGrid mySubGrid = (DataGrid) e.Item.FindControl ("dgSubCat");
if (mySubGrid == null) return;

//If needed you can find out which Item you're working on by accessing the DataItem property
DataRow dr = (DataRow)e.Item.DataItem;
string URL = dr["URL"];

DataTable mySubGridItemsTable = //Put code to set up your table here. You can either use the same data, or set up the data individually for each main category
mySubGrid.DataSource = mySubGridItemsTable;
mySubGrid.DataBind();
}

 

Spydermag68

Platinum Member
Apr 5, 2002
2,616
99
91
Are you using VS.NET for the design or are you using another editor?

One quick way with VS.NET.

I would drag and drop a Listbox from the Web Forms for each of the controls and set the properties for single select.

Double click anywhere on the form so that Page_Load sub program opens.
Create an arraylist with the options that will be part of the first list.
Bind the arraylist to the control.

Double click the top Listbox and you will enter a ListBox_SelectedIndexChanged
and based on the current index in the first listbox populate the the second listbox e with the an arraylist with the subcategories values.

Double click the lower listbox and code what you whan to happen to the page next.

--------------------

Another way of doing this is to create a user control with the above listboxes and code behind and drag and drop that into the display form.
 

SelArom

Senior member
Sep 28, 2004
872
0
0
www.djselarom.com
Originally posted by: Mark R
I'm not quite sure what you want.

Are you saying that you want a seperate sub-genre table for each item in the main genres table? That's what it looks like from your code.

Probably the easiest way to do it, is to bind the subcategories datalists at the time each one is created, by handling the ItemDataBound events fired from the main datalist. That's the way I'd do it anyway.

In the .aspx file you no longer need to specify the datasource and datamember properties. These will be specified seperately for each sub-datalist when the page runs.
Then in your code you need to add a handler for the ItemDataBound event

yeah sorry I wasn't very clear. I tried a few different ways so it's a little jumbled. Ideally, what I had at first was one table with all the main categories, then another table with all the sub categories for each main category. then I tried making a seperate table for each main category, and binding the datamember to that table (whose name is the same as the maincategory). That seems to be a better option.

i have the data in an xml file which I read on the page load. i have a datalist with a datagrid as the item in the itemtemplate. I want to bind each datagrid to the table named in the datalist's maincat... i hope that makes more sense. I will take a look at your code, it seems like it will steer me in the right direction.

thanks for taking the time to help!!

-SelArom
 

SelArom

Senior member
Sep 28, 2004
872
0
0
www.djselarom.com
thanks for your help fellas. I got it working. turns out all I had to do was set the datamember in the html source instead of the design view. works like a charm!

-SelArom
 

SelArom

Senior member
Sep 28, 2004
872
0
0
www.djselarom.com
bump for new question:

how do you name the primary key at runtime for a dataset? It's just coming out as Constraint1. I want to give it a specific name

newSubCatTable.Columns.Add(New DataColumn("subCat", System.Type.GetType("System.String")))
newSubCatTable.Columns.Add(New DataColumn("URL", System.Type.GetType("System.String")))
newSubCatTable.PrimaryKey = New DataColumn() {newSubCatTable.Columns("subCat")}

' need code to name the pk here

dsCategories.Tables.Add(newSubCatTable)

-SelArom
 

UCJefe

Senior member
Jan 27, 2000
302
0
0
You will have to access the Constraints property of the DataTable class, find the Constraint you are looking for (perhaps by iterating through looking for only UniqueConstraints) and then set the ConstraintName property. If you only have a single UniqueConstraint (ie your primary key) it's easy to just find the first UniqueConstraint and then name it. And if you're in complete control, you could just hardcode the index. Anyway, this is the concept:

dt.Constraints[0].ConstraintName = "MyPrimaryKey";