Cant find a combobox control in edittemplate in Gridview - c#

I am trying to find this control in the Gridview's edititemtemplate section.
<EditItemTemplate>
<ajaxToolkit:ComboBox ID="GridviewCategoryComboBox1" AppendDataBoundItems="true" runat="server" AutoCompleteMode="Suggest" DataSourceID="GridViewCategorySqlDataSource1" DataTextField="Name" DataValueField="Id" MaxLength="0" Style="display: inline;">
<asp:ListItem>Select Category</asp:ListItem>
</ajaxToolkit:ComboBox>
Here is the event handler where I try to fetch the control that is in the edititem template.
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs
e)
{
GridView1.EditIndex = e.NewEditIndex;
int id = (int)GridView1.DataKeys[e.NewEditIndex].Value;
ComboBox ddl = GridView1.Rows[e.NewEditIndex].Cells[1].FindControl("GridviewCategoryComboBox1") as ComboBox;
}
It returns null, no matter who I try to find it.
I also tried other variations such as this:
ComboBox ddl = GridView1.Rows[e.NewEditIndex].FindControl("GridviewCategoryComboBox1") as ComboBox;

You can use the RowDataBound event for this:
protected void GridView1_RowDataBound(object sender, GridViewEditEventArgs e) {
if (e.Row.RowType == DataControlRowType.DataRow) {
if ((e.Row.RowState & DataControlRowState.Edit) > 0) {
ComboBox ddl = (ComboBox)e.Row.FindControl("GridviewCategoryComboBox1");
}
}
}
Because it is likely that you have other code in the RowDataBound event, then this allows you to centralize all your code in that event and avoid duplicate code.

Related

Prevent DataList to Refesh for ClickEvents

I have a DataList Control with HeaderTemplate as LinkButtons.
The markup is
<asp:DataList runat="server" ID="dlYears" OnItemDataBound="dlYears_ItemDataBound">
<HeaderTemplate>
<asp:LinkButton ID="lblYear1" runat="server" OnClick="Year_Click"></asp:LinkButton>
<asp:LinkButton ID="lblYear2" runat="server" OnClick="Year_Click"></asp:LinkButton>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblValue" runat="server"Text='<%# String.Format("{0:f2}",DataBinder.Eval(((IDataItemContainer)Container).DataItem, "Value")) %>'></asp:Label>
</ItemTemplate>
</asp:DataList>
Actually this datalist contains year numbers as Headers as 2016, 2015.
Binding as
dlYearlys.DataSource = ds;
dlYearlys.DataBind();
On the ItemDataBound I'm styling the values of totals as
protected void dlYearlyTotals_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
int i = e.Item.ItemIndex;
if (val != ((Label)e.Item.FindControl("lblValue")).Text)
e.Item.ForeColor = System.Drawing.Color.Red;
}
}
On clicking the headers I'm displaying the another datalist values.
protected void Year_Click(object sender, EventArgs e)
{
LinkButton btn = sender as LinkButton;
BindAnotherDataList((Convert.ToInt16(btn.Text)));
}
This causing the page to refresh and the year values where I'm changing the colour as red is no more being displayed.
So how can I make dlYears Datalist control not to refresh and retain their colour on clicking of headers
Page Load
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BinddlYear();
// Binding Some other controls
}
}

How to get the containing grids key from within the server side DataBinding event of a RadComboBox

I'm trying to uniquely set the radcombobox datasource based on the detail grid ID value. I would like different values in the comboboxes for each detail table view.
here is the server side event code:
protected void MethodRadComboBox_DataBinding(object sender, EventArgs e)
{
RadComboBox combo = (RadComboBox)sender;
combo.DataSource = ...need key of details GridTableView DataKey value in order to set the source
}
<CommandItemTemplate>
<telerik:RadComboBox ID="MethodRadComboBox" runat="server" AutoPostBack="true"
AllowCustomText="true"
EmptyMessage="Select a Method."
DataTextField="MethodName" DataValueField="MethodName"
AppendDataBoundItems="true"
OnDataBinding="MethodRadComboBox_DataBinding"
OnSelectedIndexChanged="MethodRadComboBox_SelectedIndexChanged">
</telerik:RadComboBox>
</CommandItemTemplate>
<Columns>
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridCommandItem)
{
GridCommandItem commandItem = (GridCommandItem)e.Item;
RadComboBox combo = (RadComboBox)commandItem.FindControl("MethodRadComboBox");
combo.DataSource = e.Item.OwnerTableView.DataSource;
combo.DataBind();

Setting Button in listview Visibility to False if query result is "0"

I am building a web application in ASP.net and I have a little problem.
I have a LISTVIEW to display data from a data source, and in that listview I have included a BUTTON in every row to be visible if the result of the query in the Page_load is 0.
The Query works, but I don't know how to select the button in the query.
I have tried
ListView1.FindControl("hiddenButton").visible = false;
this is the buttons code
<asp:Button ID="hiddenButton" runat="server" CommandArgument ='<%# Eval("ProfileId") %>' Text="Add Friend" CssClass="btn btn-info pull-right" OnClick="addFriend_Click" Width="105px" allign="right"/>
But its not working.
You can do this in ItemDataBound event:-
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType==ListViewItemType.DataItem)
{
if (YourCondition)
{
Button hdn = (Button)e.Item.FindControl("hiddenButton");
hdn.Visible = false;
}
}
}
You need to associate this event handler in your mark-up(if not already done):-
<asp:ListView ID="ListView1" OnItemDataBound="ListView1_ItemDataBound">
</asp:ListView>
You can use ItemDataBound event To set Buttons visible to True/False
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
ListViewDataItem dataItem = (ListViewDataItem)e.Item;
if (e.Item.ItemType == ListViewItemType.DataItem)
{
Button hiddenButton=(Button) dataItem.FindControl("hiddenButton");
hiddenButton.Visible = false;
}
}

event attched to link button inside gridview is not working?

I have a grid in asp.net, inside the asp.net i am binding data as linkbutton when clicking on link button I need to call a method in code behind. the attached event is not woking in my code. how i can solve this?
my code is similar like this,
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton link = new LinkButton();
link.Text = e.Row.Cells[0].Text;
link.CommandArgument = "Hello";
link.Click += new EventHandler(this.onLinkClick);
e.Row.Cells[0].Controls.Add(link);
}
}
protected void onLinkClick(object sender, EventArgs e)
{
LinkButton btn = (LinkButton)(sender);
string value = btn.CommandArgument;
TextBox1.Text=value;
}
You have to call the function that binds the source to the GridView everytime in the Page Load
ex.
protected void Page_Load(object sender, EventArgs e)
{
PopulateGridView();
}
Because there is not logic for adding or not the link button(I guess you have to add it for each record) why don't you add it at design time?
<asp:GridView ID="GridView1" runat="server">
....
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">LinkButton</asp:LinkButton>
</ItemTemplate>
......
</asp:GridView>
Make sure that AutoEventWireup="true" on the page
Handle RowCommand event of GridView to handle "events" of buttons and you are adding LinkButton dynamically then Data Binding must be performed either at Page_Init or Page_Load.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton link = new LinkButton();
link.Text = e.Row.Cells[0].Text;
link.CommandArgument = "Hello";
e.Row.Cells[0].Controls.Add(link);
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
string value = e.CommandArgument.ToString();
TextBox1.Text=value;
}
You need to hook up the event handler for your dynamic button in the GridView's RowCreated event or it won't fire. Then use "FindControl" in the RowDataBound event handler. Personally, I don't like this model at all but sometimes it's unavoidable.
You should use the GridView's <asp:ButtonField> with the grid's RowCommand event. This way, you're not the one creating the dynamic control and wiring up the events.
Here's an article on how to use it.

GridView.FindControl() - works, but not properly

I have the the GridView's column looking like that:
<Columns>
<asp:TemplateField HeaderText="Opcje">
<ItemTemplate>
<asp:LinkButton runat="server" Text="Accept" ID="AcceptBtn" CommandName="Accept"/>
<asp:LinkButton runat="server" Text="Deny" ID="DenyBtn" CommandName="Deny"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
while a new row is being created, I want to change both LinkButton's CommandArgument property:
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
((LinkButton)e.Row.FindControl("AcceptBtn")).CommandArgument = myFiles[fileIndex].Name;
((LinkButton)e.Row.FindControl("DenyBtn")).CommandArgument = myFiles[fileIndex].Name;
}
The problem is, the code seems to be not changing anything, when I click on the AcceptBtn, the code below is invoked:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Accept")
{
string ss = (string)e.CommandArgument;
...
}
}
ss = "". Why ? If the page is PostedBack, both CommandArguments are cleared ?
Try setting the CommandArgument in the RowDataBound event instead of RowCreated.
you need to use rowdatabound event like..
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
((LinkButton)e.Row.FindControl("AcceptBtn")).CommandArgument = myFiles[fileIndex].Name;
((LinkButton)e.Row.FindControl("DenyBtn")).CommandArgument = myFiles[fileIndex].Name;
}
}
where do you set
myFiles[fileIndex].Name;
and each of its components?

Categories

Resources