Hi,
I have used RadComboBox inside my DetailsView. I send the value of combobox as comma separated to the database as (1,2,3,4). When I want to retrieve the data (1,2,3,4) from the database using SqlDataSource on edit mode of DetailsView, how can I set or bind the "Checked" values and display them as checked back in the combobox.
Here is what i have done:
--> WebForm1.Aspx
<%----------------------- DATA SOURCE FOR DELIVERABLE -------------------------%>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ITSConnectionString %>"
DeleteCommand="DELETE FROM [Deliverable] WHERE [DeliverableId] = @DeliverableId"
InsertCommand="INSERT INTO [Deliverable] ([DeliverableTitle],[CCIds]) VALUES (@DeliverableTitle, @CCIds)"
SelectCommand="SELECT * FROM [Deliverable] WHERE ([DeliverableId] = @DeliverableId)"
UpdateCommand="UPDATE [Deliverable] SET [DeliverableTitle] = @DeliverableTitle, [CCIds] = @CCIds WHERE [DeliverableId] = @DeliverableId">
<DeleteParameters>
<asp:Parameter Name="DeliverableId" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="DeliverableId" Type="int32" />
<asp:Parameter Name="DeliverableTitle" Type="String" />
<asp:Parameter Name="CCIds" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="DeliverableId" Type="int32" />
<asp:Parameter Name="DeliverableTitle" Type="String" />
<asp:Parameter Name="CCIds" Type="String" />
</UpdateParameters>
<SelectParameters>
<asp:QueryStringParameter Name="DeliverableId" QueryStringField="DeliverableId" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<%----------------------- DATA SOURCE FOR COMBOBOX -------------------------%>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ITSConnectionString %>" SelectCommand="SELECT [UserId], [DisplayName] FROM [Users]"></asp:SqlDataSource>
<%----------------------- THE DETAILS VIEW -------------------------%>
<asp:DetailsView ID="DetailsView1" runat="server" DefaultMode="Insert" AutoGenerateRows="False" DataKeyNames="DeliverableId" DataSourceID="SqlDataSource2" Height="50px" Width="125px" OnItemInserting="DetailsView1_ItemInserting" OnDataBinding="DetailsView1_DataBinding" OnDataBound="DetailsView1_DataBound">
<Fields>
<asp:BoundField DataField="DeliverableId" HeaderText="DeliverableId" InsertVisible="False" ReadOnly="True" SortExpression="DeliverableId" />
<asp:BoundField DataField="DeliverableId" HeaderText="DeliverableId" SortExpression="DeliverableId" />
<asp:BoundField DataField="DeliverableTitle" HeaderText="DeliverableTitle" SortExpression="DeliverableTitle"/>
<asp:TemplateField>
<ItemTemplate>
<telerik:RadComboBox ID="CCIds" Text='<% #Bind("CCIds") %>' runat="server"
DataSourceID="SqlDataSource1" DataTextField="DisplayName" DataValueField="UserId"
CheckBoxes="true"></telerik:RadComboBox>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
<asp:ButtonField Text="Insert" CommandName="Insert" />
<asp:ButtonField Text="Update" CommandName="Update" />
</Fields>
</asp:DetailsView>
--> WebForm1.Aspx.CS
public string GetCheckBoxValues(string RadComboBoxName)
{
RadComboBox rcb = (RadComboBox)DetailsView1.FindControl(RadComboBoxName);
var collection = rcb.CheckedItems;
StringBuilder sbValues = new StringBuilder();
if (collection.Count != 0)
{
foreach (var item in collection)
{
sbValues.Append(item.Value);
sbValues.Append(Delimiter);
}
if (sbValues.ToString().EndsWith(Delimiter))
sbValues.Remove(sbValues.Length - 1, 1);
}
return sbValues.ToString();
}
// Get the comma separate values and insert them into the DB.
protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
string CCIds = GetCheckBoxValues("CCIds");
if (CCIds != null)
{
e.Values["CCIds"] = CCIds;
}
}
Now how can I get the comma separated values and bound them with the combobox so that it should check only those items.
I have used RadComboBox inside my DetailsView. I send the value of combobox as comma separated to the database as (1,2,3,4). When I want to retrieve the data (1,2,3,4) from the database using SqlDataSource on edit mode of DetailsView, how can I set or bind the "Checked" values and display them as checked back in the combobox.
Here is what i have done:
--> WebForm1.Aspx
<%----------------------- DATA SOURCE FOR DELIVERABLE -------------------------%>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ITSConnectionString %>"
DeleteCommand="DELETE FROM [Deliverable] WHERE [DeliverableId] = @DeliverableId"
InsertCommand="INSERT INTO [Deliverable] ([DeliverableTitle],[CCIds]) VALUES (@DeliverableTitle, @CCIds)"
SelectCommand="SELECT * FROM [Deliverable] WHERE ([DeliverableId] = @DeliverableId)"
UpdateCommand="UPDATE [Deliverable] SET [DeliverableTitle] = @DeliverableTitle, [CCIds] = @CCIds WHERE [DeliverableId] = @DeliverableId">
<DeleteParameters>
<asp:Parameter Name="DeliverableId" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="DeliverableId" Type="int32" />
<asp:Parameter Name="DeliverableTitle" Type="String" />
<asp:Parameter Name="CCIds" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="DeliverableId" Type="int32" />
<asp:Parameter Name="DeliverableTitle" Type="String" />
<asp:Parameter Name="CCIds" Type="String" />
</UpdateParameters>
<SelectParameters>
<asp:QueryStringParameter Name="DeliverableId" QueryStringField="DeliverableId" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<%----------------------- DATA SOURCE FOR COMBOBOX -------------------------%>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ITSConnectionString %>" SelectCommand="SELECT [UserId], [DisplayName] FROM [Users]"></asp:SqlDataSource>
<%----------------------- THE DETAILS VIEW -------------------------%>
<asp:DetailsView ID="DetailsView1" runat="server" DefaultMode="Insert" AutoGenerateRows="False" DataKeyNames="DeliverableId" DataSourceID="SqlDataSource2" Height="50px" Width="125px" OnItemInserting="DetailsView1_ItemInserting" OnDataBinding="DetailsView1_DataBinding" OnDataBound="DetailsView1_DataBound">
<Fields>
<asp:BoundField DataField="DeliverableId" HeaderText="DeliverableId" InsertVisible="False" ReadOnly="True" SortExpression="DeliverableId" />
<asp:BoundField DataField="DeliverableId" HeaderText="DeliverableId" SortExpression="DeliverableId" />
<asp:BoundField DataField="DeliverableTitle" HeaderText="DeliverableTitle" SortExpression="DeliverableTitle"/>
<asp:TemplateField>
<ItemTemplate>
<telerik:RadComboBox ID="CCIds" Text='<% #Bind("CCIds") %>' runat="server"
DataSourceID="SqlDataSource1" DataTextField="DisplayName" DataValueField="UserId"
CheckBoxes="true"></telerik:RadComboBox>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
<asp:ButtonField Text="Insert" CommandName="Insert" />
<asp:ButtonField Text="Update" CommandName="Update" />
</Fields>
</asp:DetailsView>
--> WebForm1.Aspx.CS
public string GetCheckBoxValues(string RadComboBoxName)
{
RadComboBox rcb = (RadComboBox)DetailsView1.FindControl(RadComboBoxName);
var collection = rcb.CheckedItems;
StringBuilder sbValues = new StringBuilder();
if (collection.Count != 0)
{
foreach (var item in collection)
{
sbValues.Append(item.Value);
sbValues.Append(Delimiter);
}
if (sbValues.ToString().EndsWith(Delimiter))
sbValues.Remove(sbValues.Length - 1, 1);
}
return sbValues.ToString();
}
// Get the comma separate values and insert them into the DB.
protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
string CCIds = GetCheckBoxValues("CCIds");
if (CCIds != null)
{
e.Values["CCIds"] = CCIds;
}
}
Now how can I get the comma separated values and bound them with the combobox so that it should check only those items.