Hello,
I was wondering if anyone could offer any advice.
I'm currently using an entity datasource to display contents of a table in a grid view (no problem so far) however I'm trying to use the Grid View to add a record (here's where the problems begin)
I've set the ShowInsertButton = true in a Command field in the datagrid which gives me a nice link that says New but when I click it nothing happens (other than a postback)
Here's the code (the update works nicely):
<cc1:EntityDataSource ID="edsUsers" runat="server"
ProviderName="UserProvider"
EntityTypeName="DataLib.User"
UpdateMethod="Update"
InsertMethod="Insert"
DeleteMethod="Delete"
EntityKeyName="ID"
EntityKeyTypeName="System.Int16"
EnableTransaction="false"
SelectMethod="GetNonVendorAdmins">
</cc1:EntityDataSource>
<asp:GridView ID="grdUsers" runat="server" DataSourceID="edsUsers" DataKeyNames="ID" AutoGenerateColumns="False" OnRowUpdated="grdUsers_RowUpdated" OnRowUpdating="grdUsers_RowUpdating">
<Columns>
<asp:CommandField ShowEditButton="True" EditText="edit" UpdateText="update" CancelText="cancel" ShowInsertButton="True">
<
HeaderStyle HorizontalAlign="Left" />
</asp:CommandField>
<asp:BoundField DataField="Name" HeaderText="Name" >
<ControlStyle Width="95%" />
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:TemplateField HeaderText="Password">
<ControlStyle Width="95%" />
<EditItemTemplate>
<asp:TextBox ID="txtPwd" runat="server" Text='<%# Bind("Password") %>' TextMode="Password"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<span>**********</span>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" />
</asp:TemplateField>
<asp:CheckBoxField DataField="Inactive" HeaderText="Inactive">
<HeaderStyle HorizontalAlign="Left" />
<ControlStyle Width="95%" />
</asp:CheckBoxField>
</Columns>
</asp:GridView>
I've tried adding a link button that causes an insert on the Entity Data Source, whilst that mostly works it does get a little messy if the user chooses cancel. Also doing it this way feels like I'm working against the controls as opposed to working with them.
protected void lnkAddNew_Click(object sender, EventArgs e)
{
IDictionary values = new Hashtable();
// Create a new user
values.Add("ID", null);
values.Add("Name", "NewUser");
values.Add("Password", MakeNewPassword(10));
values.Add("Inactive", true);
edsUsers.Insert(values);
grdUsers.EditIndex = grdUsers.Rows.Count - 1;
}
Get the feeling I'm missing something really obvious!
Thanks in advance for any help.