How to change Repeater DataSourceID? - c#

I have a repeater and and and I set its DataSourceID .
<asp:Repeater ID="rpProducts" runat="server" DataSourceID="ItemDataSource" >
<HeaderTemplate>....</HeaderTemplate>
<ItemTemplate>
....
</ItemTemplate>
<FooterTemplate>
</FooterTemplate>
</asp:Repeater>
But I some time I want to change the DataSourceID and load data and again set the ItemDataSource.
This is my code for change the rpProducts DataSourceID .
rpProducts.DataSourceID = null;
rpProducts.DataSource = goods;
rpProducts.DataBind();
So then again I want to set the DataSourceID to ItemDataSource.
How can I do it?
I try like this. but is not working
rpProducts.DataSourceID = null;
rpProducts.DataSourceID = ItemDataSource.ToString();

Can you try with: ItemDataSource.ID
Also, both datasource and datasourceid cannot be set at same time, so you will have to set the datasource to null.
rpProducts.DataSource = null;
rpProducts.DataSourceID = ItemDataSource.ID;
rpProducts.DataBind();

Related

How can I pass Hidden Field value with CommandArgument of LinkButton?

I have one Repeater with multiple rows.each row has one LinkButton and one HiddenField.
HiddenField value is bind at time of Repeater's Event OnItemDataBound.
My Question is that How can I pass this HiddenField Field Value with CommandArgument of this LinkButton?
Following is my source code.
<asp:Repeater ID="rptServiceRequestList" runat="server" OnItemCommand="rptServiceRequestList_ItemCommand" OnItemDataBound="rptServiceRequestList_ItemDataBound">
<ItemTemplate>
<asp:LinkButton ID="btnCustomerDeposit" runat="server" Text="Pay Deposit" CommandName="DepositFees" CommandArgument='<%# Eval("ServiceRequestId") %>'>
</asp:LinkButton>
<asp:HiddenField ID="hidAmount" runat="server" />
</asp:Repeater>
Please Help me. thank you to all in advance.
Yes you can set multiple command argument or the another way is you can use FindControl("hidAmount") method of repeater .
You can use below code.
HiddenField hdnAmount = (HiddenField)rptServiceRequestList.FindControl("hidAmount");
int amnt = Convert.ToInt32(hdnAmount.Value);
You can set multiple command argument(to send hidamount along with command Argument) as:
<asp:LinkButton ID="btnCustomerDeposit" runat="server" Text="Pay Deposit" CommandName="DepositFees" CommandArgument='<%#Eval("ServiceRequestId") + "|" +Eval("HidAmount")%>'
</asp:LinkButton>
And on ItemCommand:
protected void rptServiceRequestList_ItemDataBound(Object Sender, RepeaterCommandEventArgs e)
{
string[] arg = new string[2];
arg = e.CommandArgument.ToString().Split('|'); // Split Here to seprate CommandName And Hidden Value
string YourcommandName = arg[0]; // Your Command Name
string YourHiddenValue = arg[1]; // Your Hidden Field Value
}

Gridview rowdatabound error : Multiple controls with the same ID 'hlLink' were found. FindControl requires that controls have unique ID

I have a gridview that has link and description to be rendered on the page.
written the below code in gridview in .aspx
<Columns>
<asp:TemplateField>
<ItemTemplate>
<p>
<asp:HyperLink ID="hlLink" runat="server" Target="_self"></asp:HyperLink></p>
</ItemTemplate>
<ItemTemplate>
<p>
<asp:Literal ID="litSummary" runat="server"></asp:Literal></p>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<AlternatingItemTemplate>
<p>
<asp:HyperLink ID="hlLink" runat="server" Target="_self"></asp:HyperLink></p>
</AlternatingItemTemplate>
<AlternatingItemTemplate>
<p>
<asp:Literal ID="litSummary" runat="server"></asp:Literal></p>
</AlternatingItemTemplate>
</asp:TemplateField>
</Columns>
and below in .aspx.csin gridview rowdataboundevent
protected void gvResults_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
SearchResultItem data = (SearchResultItem)e.Row.DataItem;
HyperLink hlLink = (HyperLink)e.Row.FindControl("hlLink");
Literal litSummary = (Literal)e.Row.FindControl("litSummary");
if (data.Description != null)
{
hlLink.Text = data.Title;
hlLink.NavigateUrl = data.Path.Replace("&", "&");
litSummary.Text = data.Description;
}
else
{
hlLink.Text = data.Path;
hlLink.NavigateUrl = data.Path.Replace("&", "&");
litSummary.Text = data.Path;
}
}
here SearchResultItem: is the result item that has link and description details.
First time when row bound event is called, it binds the data correctly, second time when called throws error "Multiple controls with the same ID 'hlLink' were found. FindControl requires that controls have unique IDs.
Please let me know whats error with the code.
Thanks
Problem : you are trying to create the same controls with same ID multiple times.
Solution : you need to remove the controls before creating them.
Try This:
void RemoveControls()
{
HyperLink l1 = (HyperLink)Page.FindControl("hlLink");
Literal l2 = (Literal)Page.FindControl("litSummary");
if(l1!= null)
Page.Controls.Remove(l1);
if(l2!= null)
Page.Controls.Remove(l2);
}
Solution 2: Pagination for Repeater control.
for implementing pagination in Repeater control you need to create PagedDataSource.
Try This:
PagedDataSource pds = new PagedDataSource();
pds.DataSource = ds.Tables[0].DefaultView;
pds.AllowPaging = true;
pds.PageSize = 8;//page sizes

Grid view Dropdown list data binding error

Am using the below code to bind the dropdown data from another table. And also refer that control name using rowindex. But it always return null.And also return the error message.
`Object reference not set to an instance of an object.`
Am using the two method, but both return the control name null
First code:
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Control ctrl = e.Row.FindControl("DDL_STATUS_FT"); //It always return null
if (ctrl != null)
{
DropDownList dd = ctrl as DropDownList;
DataSet7TableAdapters.sp_getall_trv_masterTableAdapter TA = new DataSet7TableAdapters.sp_getall_trv_masterTableAdapter();
DataSet7.sp_getall_trv_masterDataTable DS = TA.GetData();
dd.DataTextField = "fld_TName";
dd.DataValueField = "fld_id";
dd.DataSource = DS;
dd.DataBind();
}
}
}
Second :
In databind function
if (DS.Rows.Count > 0)
{
GridView2.DataSource = DS;
GridView2.DataBind();
foreach (GridViewRow grdRow in GridView2.Rows)
{
DataSet7TableAdapters.sp_getall_trv_masterTableAdapter TA1 = new DataSet7TableAdapters.sp_getall_trv_masterTableAdapter();
DataSet7.sp_getall_trv_masterDataTable DS1 = TA1.GetData();
// Nested DropDownList Control reference is passed to the DrdList object. This will allow you access the properties of dropdownlist placed inside the GridView Template column.
DropDownList drdList = (DropDownList)(GridView2.Rows[grdRow.RowIndex].Cells[4].FindControl("DDL_STATUS_FT"));//It always return null
// DataBinding of nested DropDownList Control for each row of GridView Control.
drdList.DataSource = DS1;
drdList.DataValueField = "fld_id";
drdList.DataTextField = "fld_TName";
drdList.DataBind();
}
}
Please help me to do this..
<asp:TemplateField ItemStyle-Width="100px" HeaderText="TYPE">
<ItemTemplate>
<asp:DropDownList ID="DDL_STATUS" runat="server" AutoPostBack="true" Enabled="false" >
</asp:DropDownList>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DDL_edit_STATUS" runat="server" AutoPostBack="true" SelectedValue='<%# Eval("fld_Type") %>'>
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="DDL_STATUS_FT" runat="server" AutoPostBack="true">
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
The DropDown "DDL_STATUS_FT" is in Footer Template..You must check it as follow..
if(e.Row.RowType == DataControlRowType.Footer)
{
DropDownList ctrl =(DropDownList)e.Row.Cells[CellIndex].FindControl("DDL_STATUS_FT");
}
Try to find your control by cell and cellIndex like...
Control ctrl = e.Row.Cells[yourCellIndex].FindControl("DDL_STATUS_FT");
EDITED2
you must have dropdownlist in aspx code in gridview item-template
you are finding using e.row.findcontrol without declaring it so abusively it return null
so, fist add dropdownlist into your gridview
here is a sample of your dropdownlist
<asp:TemplateField ItemStyle-Width="30px" HeaderText="DDL_STATUS_FT">
<ItemTemplate>
<asp:Dropdownlist ID="DDL_STATUS_FT" runat="server" />
</ItemTemplate>
</asp:TemplateField>
if you got null ctrl
Control ctrl = e.Row.FindControl("DDL_STATUS_FT"); //It always return null
then make sure in your aspx code DDL_STATUS_FT Control is runat="server"

"A field or property with the name'EncrypedStatusId' was not found on the selected data source."

Hi ,
I have added a new value EncrypedStatusId in DataNavigateUrlFields
And i am getting error as
"A field or property with the name'EncrypedStatusId' was not found on the selected data source."
but my list do has the property EncrypedStatusId which contain value
How to solve this?
ASPX:
<asp:HyperLinkField HeaderText="Status" DataTextField="AppStatus" DataNavigateUrlFields="NavigateUrl,Id,EncrypedStatusId"
SortExpression="AppStatus" DataNavigateUrlFormatString="{0}?Id={1}"></asp:HyperLinkField>
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("NavigateUrl","{0}").Replace(":", Server.UrlEncode(":")) %>'
DataTextField="AppStatus"></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
CODE BEHIND :
List<DTO> listDto;
IApplication engine;
engine = new Engine();
listDto = engine.ReadHistory(Session["UserID"].ToString());
this.dvHistory.DataSource = listDto;
this.dvHistory.DataBind();
do you have it in the gridview property datakeynames=[EncrypedStatusId,...]
Check that the column EncrypedStatusId column exists in your datasource.

Binding a DropDownList inside a DetailsView

I'm having problems trying to populate a dropdownlist from the database. When I'm trying to set the datasource I can't find the dropdown control, it's in a DetailsView so I think it might have something to do with it only being created when it's in edit mode. It still says it's in current mode when I'm editing though, so not sure what's going on there.
Here's the code from the aspx file:
<asp:DetailsView id="DetailsView1" runat="server" AutoGenerateRows="false" DataSourceID="myMySqlDataSrc" DataKeyNames="id" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" AutoGenerateInsertButton="False" >
<Fields>
<asp:TemplateField HeaderText="Region">
<ItemTemplate><%# Eval("region_name") %></ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="RegionDropdownList" runat="server" SelectedValue='<%# Bind("region_id")%>' />
</EditItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
And this is from the code behind:
ArrayList regionsList = BPBusiness.getRegions();
if (DetailsView1.CurrentMode == DetailsViewMode.Edit)
{
DropDownList ddlRegions = (DropDownList)DetailsView1.FindControl("RegionDropdownList");
if (ddlRegions != null)
{
ddlRegions.DataSource = regionsList;
ddlRegions.DataBind();
}
}
If it isn't already, place the sample from your code behind inside the DetailsView1_ModeChanged or DetailsView1_DataBound method. If it is in the DetailsView1_ModeChanging method, the mode has not actually changed yet.
EDIT: Also, make sure you set up the DataTextField and DataValueField like so:
DropDownList1.DataTextField = "TextFieldName";
DropDownList1.DataValueField = "ValueFieldName";
Also remove the SelectedValue bind; it does nothing except throw errors.
EDIT 2: If you really need to select a particular value of the dropdownlist when it first is databind, you could do something like this:
if(DropDownList1.Items.Contains(DropDownList1.Items.FindByValue("Value")))
{
DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue("Value));
}
try doing it in the itemcreated method
protected void DetailsView1_ItemCreated(object sender, EventArgs e)
{
ArrayList regionsList = BPBusiness.getRegions();
if (DetailsView1.CurrentMode == DetailsViewMode.Edit)
{
DropDownList ddlRegions = (DropDownList)DetailsView1.FindControl("RegionDropdownList");
if (ddlRegions != null)
{
ddlRegions.DataSource = regionsList;
ddlRegions.DataBind();
}
}
}
remember to set OnItemCreated="DetailsView1_ItemCreated"

Categories