Hijack DataBoundLiteralControl

E

endlesschuck

Hello,

I have a repeater which simply has the following:
<input type="checkbox" id='chkRptItem_<%#
DataBinder.Eval(Container.DataItem, "ID") %>' name="chkRptItems"
value='<%# DataBinder.Eval(Container.DataItem, "ID") %>' /><label
for='chkRptItem_<%# DataBinder.Eval(Container.DataItem, "ID") %>'><%#
DataBinder.Eval(Container.DataItem, "Name") %></label><br />

The reason it is an input and not an asp:CheckBox is that there's no
way to name all of them the same name so as to use them in javascript
(e.g. document.getElementsByName("chkRptItems")) which I do a lot as
well as in the code beside call (on a btnSave click event):

string checkedItems = Request.Form["chkRptItems"];

which gives me a nice comma-delimited list of which "items" were
checked which I throw into a stored procedure (which then parses the
delimited list and adds them to a table). When pulling the values back
from the database for editing I all I could figure out to do was the
following in the LoadMyItems method:

while (reader.Read())
{
for (int i = 1; i < rptItems.Controls.Count - 1; i++)
{
if (rptItems.Controls.Controls[0] is
System.Web.UI.DataBoundLiteralControl)
{
string controlText =
((DataBoundLiteralControl)rptItems.Controls.Controls[0]).Text;
if (controlText.IndexOf("chkRptItem_" +
Convert.ToString(reader["ID"])) > 0)
{
controlText =
controlText.Insert(controlText.IndexOf("value"), "checked='true' ");
rptItems.Controls.Controls.Clear();
rptItems.Controls.Controls.Add(new
LiteralControl(controlText));
}
}
}
}

As is, all of the above works great and exactly how I want it *Except*
when I make a round-trip. For instance, I have a delete button which,
oddly enough, deletes the main thing to which all these items belong.
But sometimes, according to our business rules this main thing can't be
deleted so I register a startup script which simply throws an alert
saying "You can't do this without fixing other stuff blah blah blah."
When the page loads up again the the checked items don't survive.
What's interesting is that the html source on the client shows that any
item that was checked now looks like:

<input type="checkbox" id='chkRptItem_' name="chkRptItems" value=''
/><label for='chkRptItem_'></label><br />

As opposed to what an unchecked item looks like:

<input type="checkbox" id='chkRptItem_3' name="chkRptItems" value='3'
/><label for='chkRptItem_3'>Item 3</label><br />

It looks as though viewstate is remembering that there's a repeater
item there but doesn't remember any of the dynamic values. I would have
thought that by deleting the DataBouldLiteralControl, then it would go
away completely so I was baffled when I saw that the repeater item was
being put back (sans necessary data). I have tried to create a new
LiteralControl and setting its EnableViewState to true and then adding
that to rptItems.Controls.Controls but that changed nothing. What I
would like to be able to do is hijack the DataBoundLiteralControl and
just replace the text but it doesn't look like that's possible.

Any ideas would be hugely appreciated,

nautonnier
 
N

nautonnier

No one? Nothing?

Hello,

I have a repeater which simply has the following:
<input type="checkbox" id='chkRptItem_<%#
DataBinder.Eval(Container.DataItem, "ID") %>' name="chkRptItems"
value='<%# DataBinder.Eval(Container.DataItem, "ID") %>' /><label
for='chkRptItem_<%# DataBinder.Eval(Container.DataItem, "ID") %>'><%#
DataBinder.Eval(Container.DataItem, "Name") %></label><br />

The reason it is an input and not an asp:CheckBox is that there's no
way to name all of them the same name so as to use them in javascript
(e.g. document.getElementsByName("chkRptItems")) which I do a lot as
well as in the code beside call (on a btnSave click event):

string checkedItems = Request.Form["chkRptItems"];

which gives me a nice comma-delimited list of which "items" were
checked which I throw into a stored procedure (which then parses the
delimited list and adds them to a table). When pulling the values back
from the database for editing I all I could figure out to do was the
following in the LoadMyItems method:

while (reader.Read())
{
for (int i = 1; i < rptItems.Controls.Count - 1; i++)
{
if (rptItems.Controls.Controls[0] is
System.Web.UI.DataBoundLiteralControl)
{
string controlText =
((DataBoundLiteralControl)rptItems.Controls.Controls[0]).Text;
if (controlText.IndexOf("chkRptItem_" +
Convert.ToString(reader["ID"])) > 0)
{
controlText =
controlText.Insert(controlText.IndexOf("value"), "checked='true' ");
rptItems.Controls.Controls.Clear();
rptItems.Controls.Controls.Add(new
LiteralControl(controlText));
}
}
}
}

As is, all of the above works great and exactly how I want it *Except*
when I make a round-trip. For instance, I have a delete button which,
oddly enough, deletes the main thing to which all these items belong.
But sometimes, according to our business rules this main thing can't be
deleted so I register a startup script which simply throws an alert
saying "You can't do this without fixing other stuff blah blah blah."
When the page loads up again the the checked items don't survive.
What's interesting is that the html source on the client shows that any
item that was checked now looks like:

<input type="checkbox" id='chkRptItem_' name="chkRptItems" value=''
/><label for='chkRptItem_'></label><br />

As opposed to what an unchecked item looks like:

<input type="checkbox" id='chkRptItem_3' name="chkRptItems" value='3'
/><label for='chkRptItem_3'>Item 3</label><br />

It looks as though viewstate is remembering that there's a repeater
item there but doesn't remember any of the dynamic values. I would have
thought that by deleting the DataBouldLiteralControl, then it would go
away completely so I was baffled when I saw that the repeater item was
being put back (sans necessary data). I have tried to create a new
LiteralControl and setting its EnableViewState to true and then adding
that to rptItems.Controls.Controls but that changed nothing. What I
would like to be able to do is hijack the DataBoundLiteralControl and
just replace the text but it doesn't look like that's possible.

Any ideas would be hugely appreciated,

nautonnier
 

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,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top