C# datagrid将项添加到数据源lose viewstate

C# datagrid将项添加到数据源lose viewstate,c#,asp.net,c#-3.0,datasource,C#,Asp.net,C# 3.0,Datasource,我有一个页面,有一个文本框、一个按钮和一个数据网格 这里的目标很简单:每次我在文本框中输入内容并按下按钮时,它都会进行一点检查,然后将该项添加到datagrid和业务逻辑数据源中 问题是,如果我想让项目出现在DataGrid中,我必须执行DataGrid.DataSource=myBusinessObject;datagrid.DataBind();问题是我丢失了viewstate和用户在radiobox中所做的选择 以下是模板: <asp:TextBox ID="

我有一个页面,有一个文本框、一个按钮和一个数据网格

这里的目标很简单:每次我在文本框中输入内容并按下按钮时,它都会进行一点检查,然后将该项添加到datagrid和业务逻辑数据源中

问题是,如果我想让项目出现在DataGrid中,我必须执行DataGrid.DataSource=myBusinessObject;datagrid.DataBind();问题是我丢失了viewstate和用户在radiobox中所做的选择

以下是模板:

            <asp:TextBox ID="txtDomain" runat="server" style="margin-bottom: 0px" 
                Width="254px"></asp:TextBox>
                &nbsp;<asp:Button ID="domainAdd" runat="server" onclick="domainAdd_Click" 
                style="height: 26px" Text="Ajouter" />
            &nbsp;<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>

因此,我想直接的问题是:如何在不丢失网格中RadioBox选项的情况下向网格和业务对象添加项。

您的数据网格将其RadioButton绑定到Test1的属性,因此如果您相应地设置这些属性,它是否应该保留所选的选项

我不是一个ASP.NET的家伙,我希望有比我更聪明的人帮助你,但因为没有人回答。我要试一试。您的单选按钮绑定到您的Test1.IsRegister属性,这意味着要保持其状态,您必须将该属性设置为true或false。首先将Test1类更改为包含bool而不是字符串。现在我将CanRegister设置为always true,您可以稍后更改逻辑

public class Test1
    {
        public string Name { get; set; }

        public bool IsRegister { get; set; }
        public bool IsTransfert { get; set; }
        public bool IsSelfManaged { get; set; }

        public bool CanRegister { get { return true; } }
        public bool CanTransfert { get { return true; } }
        public bool CanSelfManage { get { return true; } }
    }
现在,您需要在每次有人按下单选按钮时进行保存,您需要创建一个事件处理程序并将AutoPostBack设置为true

<asp:RadioButton OnCheckedChanged="register_checked" AutoPostBack="true" ID="register" runat="server" GroupName="domaine_action" Checked='<%# Bind("IsRegister")%>' Enabled='<%# Bind("CanRegister")%>' />

现在,事件处理程序寄存器已选中。这是获取单选按钮值的部分。选中属性并将其设置为列表中的项目

 protected void register_checked(object sender, EventArgs e)
        {
            //make sure you have a list
            if(Session["dic"] == null)
                return;

            List<Test1> _dic =  (List<Test1>)Session["dic"];
            RadioButton btn = sender as RadioButton;
            string btnClientId = btn.ClientID;

            //make sure cast didnt crash
            if (btn == null)
                return;

            foreach (DataGridItem control in dg1.Items)
            {
                //find the register RadioButton, ID="register"
                var item = control.FindControl("register");
                //make sure its the right RadioButton
                if (item.ClientID.Equals(btnClientId))
                {
                    //get the item index of this DataGridItem and take the appropriate object for List<Test1>
                    Test1 realItem = _dic[control.ItemIndex] as Test1;
                    if (realItem == null)
                        continue;

                    //set the Items IsRegister to the button value.
                    realItem.IsRegister = btn.Checked;
                }
            }

        }
已选中受保护的无效寄存器(对象发送方,事件参数e)
{
//确保你有一份清单
如果(会话[“dic”]==null)
返回;
列表dic=(列表)会话[“dic”];
RadioButton btn=发送方为RadioButton;
字符串btnClientId=btn.ClientID;
//确保演员没有崩溃
如果(btn==null)
返回;
foreach(dg1.Items中的DataGridItem控件)
{
//查找寄存器单选按钮,ID=“寄存器”
var item=control.FindControl(“寄存器”);
//确保它是正确的单选按钮
if(item.ClientID.Equals(btnClientId))
{
//获取此DataGridItem的项索引,并为List获取适当的对象
Test1 realItem=_dic[control.ItemIndex]作为Test1;
if(realtime==null)
继续;
//将项目IsRegister设置为按钮值。
realtItem.IsRegister=btn.已选中;
}
}
}
您必须执行所有这些操作的原因是,您的RadioButtonItemTemplate的一部分,并且绑定到DataGrid,这意味着可能会有许多DataGridItem以及许多具有相同id“register”的RadioButton。这就是为什么你不能从代码后面找到它


这将保留单选按钮的状态。希望这对您有所帮助。

在向列表中添加新项之前,您必须保存业务对象中RadioBox的状态,以便在再次绑定时,这些选择将反映回数据网格中。

如果要这样做,还不如禁用网格的ViewState…

实际上我已经尝试不执行datagrid.Datasource=“something”和datagrid.DataBind();当有回帖时。。。但对象不是以这种方式添加的……是的,它是以这种方式工作的=D不完全干净,但工作明确:)
<asp:RadioButton OnCheckedChanged="register_checked" AutoPostBack="true" ID="register" runat="server" GroupName="domaine_action" Checked='<%# Bind("IsRegister")%>' Enabled='<%# Bind("CanRegister")%>' />
 protected void register_checked(object sender, EventArgs e)
        {
            //make sure you have a list
            if(Session["dic"] == null)
                return;

            List<Test1> _dic =  (List<Test1>)Session["dic"];
            RadioButton btn = sender as RadioButton;
            string btnClientId = btn.ClientID;

            //make sure cast didnt crash
            if (btn == null)
                return;

            foreach (DataGridItem control in dg1.Items)
            {
                //find the register RadioButton, ID="register"
                var item = control.FindControl("register");
                //make sure its the right RadioButton
                if (item.ClientID.Equals(btnClientId))
                {
                    //get the item index of this DataGridItem and take the appropriate object for List<Test1>
                    Test1 realItem = _dic[control.ItemIndex] as Test1;
                    if (realItem == null)
                        continue;

                    //set the Items IsRegister to the button value.
                    realItem.IsRegister = btn.Checked;
                }
            }

        }