October 2009 - Digital Tool Factory blog October 2009 - Digital Tool Factory blog

The Digital Tool Factory Blog

How to fix dropdown boxes in asp.net datagrids

OK, this problem isn’t silverlight, but I did encounter it in my day job. In fact, I’ve encountered it several times, so I thought I would write it up so I can find it again.

The Problem: You write a web page that contains an updatable datagrid, which, when in EditMode, contains a dropdown list containing lookup items. The DropdownBox does not populate, and (later when you get it working) the proper listitem is not selected.

The Cause: Since the DropDownBox down not existing when the page loads, there is nothing to bind.

The Solution: You have to attach your events to some non-standard page events to get it to work. The relevant part of the .aspx page is below (put this in your datagrid)

<asp:TemplateColumn HeaderText=”Tactic Category”>
<HeaderStyle Font-Size=”Large” Font-Bold=”true” />
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem,”TacticCatName”)%>
</ItemTemplate>
<EditItemTemplate>
<%–Notice the GetCat() routine here as the datasource.–%>
<asp:DropDownList id=”ddTacticCatList” runat=”server” DataSource=”<%# GetCat() %>” DataTextField=”TacticCatName” DataValueField=”TacticCatID”>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>

Your code-behind page should look like this (in C#)
private void MainCode()
{
string strID = Request.QueryString[“id”].ToString();
//custom code to get the relevant dataset for the entire datgrid
DataSet ds = LMR.TacticSubCatList(strID);
//custom code to bind to the datagrid
Utility.DGDSNullCheck(ds, dgTacticSubCatList, lblTacticSubCatList, “There are no tactic subcategories in the database at this time.”);
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
MainCode();
}
}

protected void dgTacticSubCatList_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
//grab the index
dgTacticSubCatList.EditItemIndex = e.Item.ItemIndex;
MainCode();
}

protected void dgTacticSubCatList_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
//get the id value of the selected datagrid item
int intTacticSubCatID = (int)dgTacticSubCatList.DataKeys[(int)e.Item.ItemIndex];

//this gets our textbox, also in the datagrid
TextBox EditText = null;
EditText=(TextBox)e.Item.FindControl(“tbTacticSubCatName”);
string strEditText = Convert.ToString(EditText.Text);

//get the value of our dropdown list
DropDownList dd = (DropDownList)e.Item.FindControl(“ddTacticCatList”);
string strTacticCatID = dd.SelectedValue.ToString();
//do our database update
LMR.TacticSubCatEdit(intTacticSubCatID.ToString(),strTacticCatID,strEditText);
//reset the datagrid
dgTacticSubCatList.EditItemIndex = -1;
//give feedback and rebind
lblFeedback.Text = “Your changes have been applied.”;
MainCode();
}

protected void dgTacticSubCatList_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgTacticSubCatList.EditItemIndex = -1;
MainCode();
}
protected DataTable GetCat()
{
//here is where we get the dataset to populate the dropdown list – it does have to go in an independant function
DataSet ds = LMR.TacticCatList();
return ds.Tables[0];
}

protected void dgTacticSubCatList_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.EditItem)
{
//we set the selcted index in the ItemDataBound event
string strID = Request.QueryString[“id”].ToString();
string strSubCatID = dgTacticSubCatList.DataKeys[(int)e.Item.ItemIndex].ToString();
Holder.TacticSubCat tsc = LMR.GetTacticSubCat(strSubCatID);
DropDownList dd = (DropDownList)e.Item.FindControl(“ddTacticCatList”);
dd.SelectedIndex =dd.Items.IndexOf(dd.Items.FindByValue(strID));
}
}

That’s all there is to it. The tricky parts are putting the initial dropdown population in a separate function, and setting the index in the ItemDataBound event.

 

This post originally appeared on the Stronico blog – with the absorption of Stronico into Digital Tool Factory this post has been moved to the Digital Tool Factory blog


31
Oct 09


Written By Steve French

 

SEO Tip – Set your domain to expire in several years

I was recently looking into more Search Engine Optimization techniques and came across several experts (such as they are) saying that Google ranks sites with domain names that expire in a year or less lower than sites who’s domain names expire after that. While that doesn’t seem very fair, it does make sense. Many PR and spam sites aren’t intended to last for long, and a distant expiration date does signal that the site is meant to be around for a long while.

On that note, I just renewed Stronico for another three years.

 

This post originally appeared on the Stronico blog – with the absorption of Stronico into Digital Tool Factory this post has been moved to the Digital Tool Factory blog


27
Oct 09


Written By Steve French

 

Google Desktop is now available for Vista Ultimate 64 bit

It wasn’t before. Hallelujah! Just after Windows 7 comes out too.

 

This post originally appeared on the Stronico blog – with the absorption of Stronico into Digital Tool Factory this post has been moved to the Digital Tool Factory blog


26
Oct 09


Written By Steve French

 

How to fix margins in silverlight

The Problem: You try to set margins the same way in xaml as in xhtml/css.

The Cause:
The margin property in xaml is different than the margin in xhtml/css

The Solution:
Use this bit of info: the margins in xaml go left,top,right,bottom, or clockwise from 9:00.

This was hardly the most significant problem I had with the new app, but it did take me a little while to figure out, so I thought I would blog it in case someone else had the same problem.

 

This post originally appeared on the Stronico blog – with the absorption of Stronico into Digital Tool Factory this post has been moved to the Digital Tool Factory blog


25
Oct 09


Written By Steve French

 

How to fix “Value does not fall within the expected range” error in Silverlight

The Problem: Your code goes about it’s merry way, adding children to parents and so forth when you get the error

Value does not fall within the expected range

The Cause: The problem is caused by two objects having the same name in the same parent. If your canvas has two StackPanels named “spOverlap” then this error is thrown. Why the error message couldn’t just say “Duplicate Name Error” no one knows. I’ve encountered this error when dynamically adding controls to the application.

The Solution (Part 1): The simple way to fix it is to simply name the newly added control with a guid or a ID number of some sort.

The Cause (Part 2): I did in fact do that, and then encountered another error, namely that things were expanding exponentially.

Here’s the situation. I have a main control (call it StronScreen), which will spawn a child control (call it StronTab), which has an event (call it StronEntityAdd) that will add a control (call it StronEntity) to StronScreen. I was adding the event to StronTab every time I spawned it, which meant that there were multiple events attached to StronTab as the user went through the application. It worked fine the first time, by which I mean that it added a single instance of StronEntity to StronScreen. The next time I spawned StronTab (this is a necessary and desirable feature of the application) and call StronEntityAdd TWO instances of StronEntity would be added to StronScreen.

The Solution (Part 2): Create the event handler as null when the page is loaded, and when StronTab is called, check to see if the event is null, and if it is, add it to StronTab. If the event handler is not null, then simply don’t add it.

A hard problem to diagnose, and the application does have to work that way, but that fixes it.

 

This post originally appeared on the Stronico blog – with the absorption of Stronico into Digital Tool Factory this post has been moved to the Digital Tool Factory blog


24
Oct 09


Written By Steve French

 

The Strength of Weak Ties

One of the principal theories of the Stronico application is known as “The Strength of Weak Ties“. The link is a nice explanation of that, “Never Eat Alone” by Keith Ferrazzi is a book-length treatise on the theory and the application of that.

As it’s one of the fundamental theories, I thought I would link it here.

 

This post originally appeared on the Stronico blog – with the absorption of Stronico into Digital Tool Factory this post has been moved to the Digital Tool Factory blog


24
Oct 09


Written By Steve French

 

How to solve Unhandled Error in Silverlight Application Code: 2104

I’ve found that I get this error every time I upload a Silverlight application to a new webserver. IIS does not have the correct settings by default.

The Problem: You ftp a Silverlight App to an IIS website, go to the website in a browser, and get the following error:

Message: Unhandled Error in Silverlight Application
Code: 2104
Category: InitializeError
Message: Could not download the Silverlight application. Check web server settings

The Cause: The Silverlight Mime types (.xaml, .xap and .xbap) are not listed in that websites registered mime types, therefore IIS will not send those files to the browser.

The Solution: Open IIS on the server, go to HTTP headers, click on Mime Types, click “Add New” and add the following:

Extension – Mime Type
.xaml – application/xaml+xml
.xap – application/x-silverlight-app
.xbap – application/x-ms-xbap

Hit Apply and your application will load!

 

This post originally appeared on the Stronico blog – with the absorption of Stronico into Digital Tool Factory this post has been moved to the Digital Tool Factory blog


23
Oct 09


Written By Steve French

 




Copyright 2011 Digital Tool Factory. All Rights Reserved. Powered by raw technical talent. And in this case, WordPress.