views:

433

answers:

1

I have a list view with a label that shows "Step #x", another label with the instructions, and then 2 linkbuttons for editing or deleting. At the footer of the ListView is another button "Add New Step" which opens up an empty TextBox and two buttons to save and cancel. I would also like it to increment the label, but I can seem to find the control in the code behind to change it's text. How do I do this?

Here's the asp markup:

<asp:ListView ID="lvSteps" runat="server" DataSourceID="ldsProcessStep" DataKeyNames="ID" InsertItemPosition="None">
    <LayoutTemplate>
        <div><asp:PlaceHolder ID="itemPlaceholder" runat="server" /></div>
        <asp:Button ID="btnAddNewStep" runat="server" Text="Add New Step" OnClick="btnAddNewStep_Click" />
    </LayoutTemplate>
    <ItemTemplate>
        <table width="100%">
            <tr>
                <td>
                    <asp:label runat="server" Text='<%# Eval( "StepNumber", "Step #{0}" ) %>' Width="75px" style="font-size:medium; font-weight:bold;" />
                </td>
                <td>
                    <div style="text-align:left; width:350px;">
                        <asp:label runat="server" Text='<% #( Eval("Instruction") ) %>' style="font-size:85%;" />
                    </div>
                </td>
                <td>
                    <div style="width:50px;">
                        <div><asp:LinkButton Text="Edit" runat="server" CommandName="Edit" style="font-size:75%;" /></div>
                        <div style="margin-top:5px;"><asp:LinkButton Text="Delete" runat="server" style="font-size:75%;" OnClientClick='<%# CreateConfirmation( Eval("StepNumber") ) %>' /></div>
                    </div>
                </td>
            </tr>
        </table>
        <hr style="width:90%; margin-left:20px;" />
    </ItemTemplate>
    <InsertItemTemplate>
        <table width="100%">
            <tr>
                <td>
                    <asp:Label ID="lblNewStepNumber" runat="server" Width="75px" Text="????" style="font-size:medium; font-weight:bold;" />
                </td>
                <td>
                    <div style="text-align:left; width:350px;">
                        <asp:TextBox ID="txtInstruction" runat="server" TextMode="MultiLine" Rows="3" Width="100%" Text='<%# Bind("Instruction") %>' style="font-size:85%;" />
                    </div>
                </td>
                <td>
                    <div style="width:50px;">
                        <div><asp:LinkButton ID="btnInsert" Text="Save" runat="server" CommandName="Insert" style="font-size:75%;" /></div>
                        <div style="margin-top:5px;"><asp:LinkButton ID="lnkCancelInsert" Text="Cancel" runat="server" CommandName="Cancel" OnClick="btnCancelInsert_Click" style="font-size:75%;" /></div>
                    </div>
                </td>
            </tr>
        </table>
    </InsertItemTemplate>
</asp:ListView>

And some attempted code:

public void btnAddNewStep_Click( object sender, EventArgs e )
{
    lvSteps.InsertItemPosition = InsertItemPosition.LastItem;
    lvSteps.FindControl( "btnAddNewStep" ).Visible = false;

    //Cannot find control

    //lvSteps.FindControl( "lblNewStepNumber" ).Text = "doesn't work"

    //Label lbl = (Label)lvSteps.FindControl( "lblNewStepNumber" );
    //lbl.Text = "Doesn't work"'
}
+2  A: 

Hey,

I believe lvSteps has a reference to InsertItem (as in lv.InsertItem.FindControl("")), which you can use to find controls in the insert template. For lvSteps.FindControl finds controls created in the layout template. I think ItemDataBound or ItemCreated may also fire for the insert item, but I'm not 100% sure about that.

Property definition is available here: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listview.insertitemtemplate.aspx

HTH.

Brian
Using the lvSteps.InsertItem.FindControl(""), I still get the "Object reference not set to an instance of an object"
Justen
OK, then you could try the itemcommand or itemdatabound event handler, and access e.Item to get to the insert item, I believe that is an option too... I'm not sure why InsertItem is null, but I know that you have to be careful when accessing it. InsertItem may only be available when bound to the list I guess.... Check out the documentation link above for more on that property; didn't see anything about null being an issue. Definitely have to access it after setting insertitemposition though...
Brian
Is there maybe an Eval I can put as my label text? like, <asp:Label ID="lbl1" runat="server" Text='<% #Eval("1 + lvSteps.Items.Count/3") %>' I'm not quite sure how to code the databound or item command, since the lv is doing neither. The button is just making the insertitemtemplate viewable, no data is saved.
Justen
No eval support because the insert item doesn't have a data item available for it. In itemcreated or itemdatabound, you should be able to do: if (e.Item.ItemType == ListViewItemType.InsertItem) then do insert code. That's how you can target the insert item, by the insert item type. The inserted row would be in the event argument, e.Item.
Brian
I went back and checked one of my pages. I accessed insertitem property directly when data binding; I also accessed it in itemdatabound, so it must only be available when data binding. Also, if you wanted to not use itemdatabound, you should be able to access InsertItem in PreRender, provided you bound the listview before this.
Brian
Yeah, I still can't quite get it. I made a function for OnItemDataBound, did the if(), and tried to make it find the label, which didn't yield an error, but didn't change the text
Justen
That's weird, it should... you could also try the onprerender method in the page, and do: if (lv.InsertItem != null) { // set label text
Brian