I do have a page that has a TextBox a Button and a Datagrid.
The goal here is simple : Each time I input something in the TextBox and press the Button, it does a little check and then it adds the item to the datagrid and a business logic datasource.
The problem is, if I want to make the item appear in the DataGrid I have to do a datagrid.DataSource = myBusinessObject; datagrid.DataBind(); problem with that is I lose the viewstate and the choice in the radiobox the user have done.
Here is the Template :
<asp:TextBox ID="txtDomain" runat="server" style="margin-bottom: 0px"
Width="254px"></asp:TextBox>
<asp:Button ID="domainAdd" runat="server" onclick="domainAdd_Click"
style="height: 26px" Text="Ajouter" />
<br />
<br />
<asp:DataGrid ID="dg1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundColumn DataField="Name" HeaderText="Nom choisi"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Enregistrer">
<ItemTemplate>
<asp:RadioButton ID="register" runat="server" GroupName="domaine_action" Checked='<%# Bind("IsRegister")%>' Enabled='<%# Bind("CanRegister")%>' />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Transférer">
<ItemTemplate>
<asp:RadioButton ID="transfert" runat="server" GroupName="domaine_action" Checked='<%# Bind("IsTransfert")%>' Enabled='<%# Bind("CanTransfert")%>' />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Gérer moi même">
<ItemTemplate>
<asp:RadioButton ID="manage" runat="server" GroupName="domaine_action" Checked='<%# Bind("IsSelfManaged")%>' Enabled='<%# Bind("CanSelfManage")%>' />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
The code behind :
protected void domainAdd_Click(object sender, EventArgs e)
{
if (Session["dic"] != null)
{
var _dic = (List<Test1>)Session["dic"];
_dic.Add(new Test1 { Name = txtDomain.Text });
dg1.DataSource = _dic;
dg1.DataBind();
Session["dic"] = _dic;
}
else
{
List<Test1> _dic = new List<Test1>();
_dic.Add(new Test1 { Name = txtDomain.Text });
dg1.DataSource = _dic;
dg1.DataBind();
Session["dic"] = _dic;
}
For now the Test1 object is just a little class to map fields for business logic.
public class Test1
{
public string Name { get; set; }
public string IsRegister { get; set; }
public string IsTransfert { get; set; }
public string IsSelfManaged { get; set; }
public string CanRegister { get; set; }
public string CanTransfert { get; set; }
public string CanSelfManage { get; set; }
}
So I guess the direct question would be : How to add items to the grid & the business object without loosing the choices of radioboxes in the grid.
You’re going to have to save the state of the radioboxes in your business object before adding a new item the List, so that when you bind it again, the choices will be reflected back in the datagrid.
If you’re doing that, you might as well disable the ViewState for the grid…