开发者

Creating dynamic DataList controls with ID's based on bound data

开发者 https://www.devze.com 2023-01-04 18:47 出处:网络
As a workaround for the fact that asp:Checkboxes don\'t have values, I am attempting to dynamically create the ID\'s of checkboxes in a DataList so that i开发者_运维技巧t inserts the primary keys into

As a workaround for the fact that asp:Checkboxes don't have values, I am attempting to dynamically create the ID's of checkboxes in a DataList so that i开发者_运维技巧t inserts the primary keys into the control ID. This is surprisingly difficult.

I have placed a PlaceHolder in my DataList ItemTemplate, then in the ItemCreated I create the checkboxes using string.Format("Checkbox{0}", DataBinder(e.Item.DataItem, "ID")). The problem is that this only works in a non-postback condition, as on postback the DataItem is null. And of course ItemDataBound isn't called on PostBack so that won't work either.

I can't seem to find a good way to handle this short of if (IsPostback) dataList.Bind(), which i don't think is a good way to do it.

Can anyone provide me with any alternatives here?

EDIT:

Some additional information. I just realized that part of the problem was because I actually have a DataList within a DataList. The reason DataItem is null is because there is no databinding on postback, and the child data is not saved to viewstate.

Basically, what i'm doing is This, although it's using a DataList rather than Repeater. So, on postback, the Children collection doesn't get set because ItemDataBound isn't called on postback.

EDIT2: To clarify, the problem is largely because of the nested datalists. I have to set the datasource of the nested datalist to a collection field of the first datalist's individual rows fields. On postback, there is no databinding, so it doesn't work.


You could use a similar technique to the one I wrote up in this answer - add a regular CheckBox, and a HiddenField control in the ItemTemplate, and bind the HiddenField to the primary key value e.g.

<ItemTemplate>
    <tr>
        <td>
            <asp:CheckBox runat="server" ID="MyCheckBox" AutoPostBack="true" oncheckedchanged="MyCheckBox_CheckedChanged"  />
            <asp:HiddenField runat="server" id="DatabaseKeyHiddenField" Value='<%# Eval("DatabaseKey") %>' />
        </td>
    </tr>
</ItemTemplate>

protected void MyCheckBox_CheckedChanged(object sender, EventArgs e)
{
    CheckBox selectedCheckBox;
    DataListItem selectedDataListItem;
    HiddenField databaseKeyHiddenField;
    string databaseKey;

    // Cast the sender object to a CheckBox
    selectedCheckBox = (CheckBox)sender;

    // Walk up the tree one level so we get the container for both controls
    selectedDataListItem = (DataListItem)selectedCheckBox.Parent;

    // Get the HiddenField control ...
    databaseKeyHiddenField = (HiddenField)selectedDataListItem.FindControl("DatabaseKeyHiddenField");

    // ... and read the value
    databaseKey = databaseKeyHiddenField.Value;

    // Go off and do a database update based on the key we now have
    ...
}

It's a bit of a workaround rather than exactly what you want to do, but it works!

0

精彩评论

暂无评论...
验证码 换一张
取 消