Avatar

I know there's a better way to do this.

1
2
3
4
5
6
7
8
9
10
11
<select id="<%Response.Write(QuantityId); %>" onmousewheel="return(false);" name="<%Response.Write(QuantityId); %>"
	onchange="javascript:<%Response.Write(ChangeHandler); %>">
	<%
		for (int i = CanRemove ? 0 : 1; i <= 9; ++i)
		{
			string select = "";
			if (StartQuantity == i) {select = "selected=\"selected\"";}
			Response.Write(string.Format("\n<option value=\"{0}\" {1} >{0}</option>", i,select));			
		}
	%>
</select>

Refactorings

No refactoring yet !

49c552e21b2fcbff0ff6567507ef1715

Cohen

June 9, 2009, June 09, 2009 13:04, permalink

No rating. Login to rate!

Where to start....
Is it asp.net webforms or ASP.NET MVC?
If it is webforms why not use the dropdownlist control?
If it is MVC why not use the HtmlHelpers?

Avatar

emptyiness12z.blogspot.com

June 10, 2009, June 10, 2009 14:07, permalink

No rating. Login to rate!

It's webforms.

Avatar

sandro-ciervo.myopenid.com

June 15, 2009, June 15, 2009 14:49, permalink

No rating. Login to rate!

Why not using the DropDownList Control?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//
// ASPX
//
<form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="MyDropDownList" runat="server" />
    </div>
</form>

//
// CodeBehind
//
protected void Page_Load(object sender, EventArgs e)
{
    //..

    for (int i = CanRemove ? 0 : 1; i <= 9; ++i)
    {
        // Create a ListItem
        ListItem li = new ListItem();
        li.Value = i.ToString();
        li.Text = i.ToString();
        li.Selected = (i == StartQuantity);

        // Add ListItem object to DropDownList
        MyDropDownList.Items.Add(li);
    }

    //..
    
}

Your refactoring





Format Copy from initial code

or Cancel