0

我正在用 gridview 做一些复杂的事情,而我和完成之间唯一的事情是我无法以编程方式设置数据绑定。除了我仍然无法更新数据源之外,我已经解决了这个问题。

我已经用一个依赖下拉列表替换了一个输入字段,并让它保留数据绑定上字段的值。我不能直接对它进行数据绑定,因为我得到“选定的值不在项目列表中”或其他东西,所以我必须找到一种方法来让网格或数据源从这个依赖下拉列表中获取值并将其应用到桌子。

有什么帮助吗?

<asp:GridView ID="gvManager" runat="server" 
        AutoGenerateColumns="False" DataSourceID="ldsCampaigns" 
        ondatabound="gvManager_DataBound" 
        onrowediting="gvManager_RowEditing" DataKeyNames="ContentID">
        <Columns>
            <asp:CommandField ShowEditButton="True" />
            <asp:BoundField DataField="ContentID" HeaderText="ContentID" 
                SortExpression="ContentID" />
            <asp:BoundField DataField="CampaignName" HeaderText="CampaignName" 
                SortExpression="CampaignName" />
            <asp:BoundField DataField="CampaignTitle" HeaderText="CampaignTitle" 
                SortExpression="CampaignTitle" />
            <asp:BoundField DataField="CampaignTagLine" HeaderText="CampaignTagLine" 
                SortExpression="CampaignTagLine" />
            <asp:TemplateField HeaderText="CampaignData" SortExpression="CampaignData">
                <EditItemTemplate>
                    <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="ldsTables" 
                        DataTextField="CMSTables" DataValueField="CMSTables" 
                        SelectedValue='<%# Bind("CampaignData") %>' onload="DropDownList1_Load">
                    </asp:DropDownList>
                    <asp:LinqDataSource ID="ldsTables" runat="server" 
                        ContextTypeName="DataContext" 
                        onselecting="ldsTables_Selecting" Select="new (CMSTables)" 
                        TableName="CampaignTables">
                    </asp:LinqDataSource>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("CampaignData") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="DataColumn" 
                SortExpression="DataColumn">
                <EditItemTemplate>
                    <asp:DropDownList ID="DropDownList2" runat="server" style="margin-bottom: 0px" 
                        SelectedValue='<%# Bind("DataColumn") %>' Visible="False">
                    </asp:DropDownList>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" 
                        Text='<%# Bind("DataColumn") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="CampaignLink" HeaderText="CampaignLink" 
                SortExpression="CampaignLink" />
            <asp:BoundField DataField="Sunrise" HeaderText="Sunrise" 
                SortExpression="Sunrise" />
            <asp:BoundField DataField="Sunset" HeaderText="Sunset" 
                SortExpression="Sunset" />
            <asp:BoundField DataField="OwnerID" HeaderText="OwnerID" 
                SortExpression="OwnerID" />
            <asp:CheckBoxField DataField="Enabled" HeaderText="Enabled" 
                SortExpression="Enabled" />
        </Columns>
    </asp:GridView>
4

2 回答 2

0

如果我理解你的问题是正确的,你有一个 gridview 并且在 gridview 中有一个下拉列表,在更改下拉列表时你想要获取它的值并获取一些数据或你想要的任何东西,如果是这样的话,那么你有什么如下:

1-设置您的下拉列表属性 AutoPostBack = "true"。

2-在下拉列表的 dropdownlist_indexchanged 事件上执行以下操作:

protected void dropdownlist1_indexchanged(sender object,eventargs e)
{
   //Because the dropdownlist is inside the gridview you can't get data directly
   //You will have to parse the sender object which will retrieve the dropdownlist
   DropdownList ddl = (DropdownList)sender;
   //You can then extract the data from the dropdownlist
}
于 2012-05-30T11:56:59.927 回答
0

对于任何未来绝望的程序员,我必须在更新事件上更新 linq 数据源代码:

    protected void ldsCampaigns_Updated(object sender, LinqDataSourceStatusEventArgs e)
    {
        using (DashboardDataContext c = new DashboardDataContext())
        {
            Label Label3 = gvManager.Rows[gvManager.EditIndex].FindControl("Label4") as Label;

            List<CMSCampaigns> change = c.CMSCampaigns.Where(s => s.ContentID == Convert.ToInt16(Label3.Text)).ToList();

            DropDownList DropDownList2 = gvDashboardManager.Rows[gvDashboardManager.EditIndex].FindControl("DropDownList2") as DropDownList;


            change[0].DataColumn = DropDownList2.SelectedValue.ToString();

            c.SubmitChanges();
        }
    }

这是我用过 kludgestergrammed 的最集群的 clusterkludge。

于 2012-05-30T14:18:48.113 回答