Get Value of Non-ASP.NET Control

J

Jonathan Wood

Greetings,

I have a ListBox on my ASPX page and I need to insert non-breaking spaces in
the list items.

Therefore, I found it necessary to render the list from code rather than
using the ASP.NET ListBox control.

This works fine. But how can I retrieve the selected value of that control?

Thanks for any tips!

Jonathan
 
J

Jonathan Wood

What are you actually doing...? Please show some code...

Did I explain it that poorly?

I'm rendering a listbox from code rather than using the ASP.NET ListBox
control. So I write <select> and <option> tags to the output.

My mark up includes:

<% RenderList(); %>

And then I define RenderList() to make calls to Response.Write(), creating
the proper markup.

But it's hard to see how that matters because I could've created the tags in
a loop or any of a number of different ways.

The issue is that controls rendered via code do not have properties that can
be accessed from ASP.NET code. So how do I know which option was selected?

I plan to detect the selected item from javascript for other purposes. I
just don't know how to get this information back to my server code.

Thanks.

Jonathan
 
J

Jonathan Wood

Hmm - any particular reason...?

Yes, the one I described in my original post.
Response.Write???? Is this ASP Classic, then...?

If it were ASP classic, I wouldn't keep describing it as ASP.NET in the
subject and text of my posts.

Were you just going to express disbelief, or did you have a comment?
I really have no idea what you're trying to do - once again, please show
your code...

I really think I have addressed this more than you are giving me credit for.
I'm happy to post some specific part of the code if you ask.

Otherwise, here's the page.

<%@ Page Language="C#" MasterPageFile="~/Default.master" Title="Submit PAD
File" %>
<%@ Import Namespace="SoftCircuits" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<%@ Register src="Controls/ErrorMessage.ascx" tagname="ErrorMessage"
tagprefix="uc1" %>

<script runat="server">

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
try
{
object obj = Session["PadData"];
if (obj == null)
{
lblPrompt.Text = "Whoops! No PAD data found!";
throw new Exception("This page cannot be accessed in this context");
}
Session.Remove("PadData");
ViewState["PadData"] = obj;
PadData pad = (PadData)obj;

lblPrompt.Text = HttpUtility.HtmlEncode(String.Format("The category
\"{0}\" did" +
" not match any available categories. Please select a category for
{1}:",
pad.CategoryStr, pad.Title));
}
catch (Exception ex)
{
ErrorMessage1.SetError("Unable to initialize page", ex);
btnSubmit.Enabled = false;
}
}
}

protected void RenderList()
{
try
{
// Note: We need to render listbox manually do allow unencoded "&nbsp;"
Response.Write("<select size=\"20\" id=\"lstCategories\">\r\n");
Response.Write("<option selected=\"selected\" value=\"-1\">&lt;Please
choose a category for this submission...&gt;</option>\r\n");

using (SqlDataReader rdr =
DataHelper.ExecProcDataReader("GetCategories"))
{
int category = -1;

while (rdr.Read())
{
int thiscategory = (int)rdr["CategoryID"];
if (category != thiscategory)
{
category = thiscategory;
Response.Write(String.Format("<option value=\"-1\">{0}</option>\r\n",
HttpUtility.HtmlEncode((string)rdr["Category"])));
}
Response.Write(String.Format("<option
value=\"{1}\">&nbsp;&nbsp;&nbsp;&nbsp;{0}</option>\r\n",
HttpUtility.HtmlEncode((string)rdr["Title"]), rdr["SubcategoryID"]));
}
}
Response.Write("</select>\r\n");
}
catch (Exception ex)
{
ErrorMessage1.SetError("Unable to render category list", ex);
}
}

protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
object obj = ViewState["PadData"];
if (obj == null)
throw new Exception("Page cannot be accessed in this context (cannot
find PAD data");
PadData pad = (PadData)obj;

// TODO: Continue submission
// pad.Category = lstCategory;
}
catch (Exception ex)
{
ErrorMessage1.SetError("Unable to submit PAD file", ex);
}
}

</script>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1"
Runat="Server">

<blockquote>
<span class="head1">Select Category</span><br /><br />
<div align="justify">
<p>
<asp:Label ID="lblPrompt" runat="server" Text="Label"></asp:Label>
</p>
</div>
</blockquote>

<br />

<div class="box" style="text-align: center">
<h2>PAD File Submission</h2>

<uc1:ErrorMessage ID="ErrorMessage1" runat="server" />

<p>
Categories:<br />
<% RenderList(); %>
</p>

<p>
<asp:Button ID="btnSubmit" runat="server" Text="Submit"
onclick="btnSubmit_Click" />
</p>
</div>

</asp:Content>

Jonathan
 
J

Jonathan Wood

<asp:ListBox ID="lstTest" runat="server" />

lstTest.Items.Add(new ListItem("Please choose a category for this
submission...", "-1"));
lstTest.Items.Add(new
ListItem(HttpUtility.HtmlDecode("&nbsp;&nbsp;&nbsp;&nbsp;") + "First
option", "1"));
lstTest.Items.Add(new
ListItem(HttpUtility.HtmlDecode("&nbsp;&nbsp;&nbsp;&nbsp;") + "Second
option", "2"));
lstTest.Items.Add(new
ListItem(HttpUtility.HtmlDecode("&nbsp;&nbsp;&nbsp;&nbsp;") + "Third
option", "3"));
lstTest.Items.Add(new
ListItem(HttpUtility.HtmlDecode("&nbsp;&nbsp;&nbsp;&nbsp;") + "Fourth
option", "4"));

Okay, cool. Hadn't really thought in terms of that approach.

I like the approach I was taking for some things because it's simple and
allows me to do anything I want. But it can definitely cause problems and
I'll avoid it if it isn't needed--as appears to be the case here.

Thanks.

Jonathan
 
J

Jonathan Wood

I don't know if I see that there's always a right and wrong choice in
development.

For me, the approach I had taken is very simple and easy to understand. If
parts of ASP.NET are not yet fully understood, then the result can be easier
and more reliable.

More importantly, it seems more efficent--considerably. I had one case where
I basically wanted to dump a bunch of data in the middle of the page. I
mean, I could load it into memory using StringBuilder and then copy it to a
Literal control. Possibly, I could also use a GridView control with it's
huge arrays of allocated memory objects, but that would not improve
efficiency.

Don't get me wrong. I will change the code I posted and implement it using
your suggestions. But if I just need to dump a bunch of data into a page,
neither a Literal or GridView control seems to provide overwhelming
benefits.

Jonathan
 
J

Jonathan Wood

No, you are incorrect here, at least as regards reliability... I
appreciate, though, that Response.Write() may be easier for you to
understand...

You don't think that, if it's easier for me to understand, that I have a
greater chance of implementing it reliable? That's all I was saying.
As mentioned, Response.Write in ASP.NET is no the same as it was in ASP
Classic.In ASP.NET, Response.Write() will write content to the output at
the point that it occurs in the code, which will mean that it will happen
before the PageRender event.

Even if I call it from within the mark up, as I was?

Unless you're extremely careful or very lucky, chances
are that the text will show up at the top of the page, above all other
content rather than showing up inline as in ASP Classic. This is part of
the trade-off of moving from linear scripting code to compiled,
object-orientated code.

I've called Response.Write and no what you are talking about. But I have
never seen anything resembling that behavior when the code is called from
the mark up, as it was in the code I posted.
The point here is that if you want to be able to control where the text
shows up, Response.Write is not a good idea in ASP.NET.

Then ehy does the code I posted always work exactly as expected?

Jonathan
 
J

Juan T. Llibre

re:
!> if you're even considering using Response.Write to create controls in ASP.NET,
!> you can pretty much take it as read that it's the wrong solution...

Getting rid of the "procedural state of mind" inherent in classic ASP,
when taking up ASP.NET programming, is one of the toughest tasks
that aspiring ASP.NET programmers must come to terms with when
they come from a classic ASP programming background.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,155
Latest member
JuliW73391
Top