How to use DataBinding event in DataList - asp.net

I am using DataList and want to bind data through databinding event.
When i tried to bind data using ItemdatBound event than its give error and when i am using breakpoint than it was first go to the datBinding event and than source code and when its goes to the source code, its give error that "System.Data.DataRowView' does not contain a property with the name 'iParentPageId_PK"
So What I do Now???
Its My Code...
protected void dlRight_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item)
{
Label lblId = (Label)e.Item.FindControl("lblId");
SubPage objEntity = new SubPage()
{
ParentPageId = Convert.ToInt32(lblId.Text)
};
dlRight.DataSource = objSubBll.GetByParentPageId(objEntity);
dlRight.DataBind();
}
}
protected void dlRight_DataBinding(object sender, EventArgs e)
{
}

Related

Changing the columns titles on a GridView if I set the datasource on codebehind?

How do I set the GridView Category Columns Titles manually if I'm databinding manually?
namespace Workforce
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var s = Work.DataLayer.Connection("test");
var x = Work.DataLayer.GetCourseList(s);
GridView1.DataSource = x;
GridView1.DataBind();
}
}
}
In the design view, there is a data source id which i'm not using.
How about this
GridView1.HeaderRow.Cells[0].Text = "New Header";
Like #Rahul stated, you want to do this in the RowDataBound event
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[0].Text = "Your Better Column Header";
}
}
And repeat for each row header you want to modify, ensuring you have used the correct ordinal number.

GridView | drop down lists gets unpopulate

The Drop Down Lists in the FooterTemplate gets unpopulate when clicking "update" on some row
this is the page load event when they gets populate:
protected void Page_Load(object sender, EventArgs e)
{
DropDownList ddlImages_new = ((DropDownList)gvAdminArticleAdd.FooterRow.FindControl("ddlImages_new"));
ddlImages_new.DataSource = GetPdfs();
ddlImages_new.DataBind();
DropDownList ddl_invNamesNew = ((DropDownList)gvAdminArticleAdd.FooterRow.FindControl("ddl_invNamesNew"));
ddl_invNamesNew.DataSource = GetInvestigatorNames();
ddl_invNamesNew.DataBind();
}
If I click the update linkButton on some row the data in the drop down lists are disappear
Even when try to call the page load on cancel event it didn't work.
protected void gvAdminArticleAdd_CancelEditEventHandler(object sender, GridViewCancelEditEventArgs e)
{
Page_Load(sender, e);
}
Bind your controls only when the page is not post back:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownList ddlImages_new = ((DropDownList)gvAdminArticleAdd.FooterRow.FindControl("ddlImages_new"));
ddlImages_new.DataSource = GetPdfs();
ddlImages_new.DataBind();
DropDownList ddl_invNamesNew = ((DropDownList)gvAdminArticleAdd.FooterRow.FindControl("ddl_invNamesNew"));
ddl_invNamesNew.DataSource = GetInvestigatorNames();
ddl_invNamesNew.DataBind();
}
}

Initial selection for a DropDownList overriding user selection

I'm trying to set an initial selection for a DropDownList by calling: drop.SelectedIndex = 5 in Page_Load.
This works, but then if I change the selection manually and want to save the form, I'm still getting the initial selection instead of the new selection when calling drop.SelectedValue. What's wrong?
You have forgotten you check if(!IsPostback). Otherwise you will select the 6th item again on postbacks before the SelectedIndexChanged event is triggered (or a button-click event):
protected void Page_Load(Object sender, EventArgs e)
{
if(!IsPostBack) // do this only on the initial load and not on postbacks
dropDwonList1.SelectedIndex = 5;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//set up data here
}
}
if (Page.IsPostBack)
{
//do page reload logic in here
}
protected void foo(object sender, EventArgs e)
{
//get your selected value here
}
Try this code
You should be using if(!IsPostback) in the Page_Load function.
protected void Page_Load(Object sender, EventArgs e)
{
if(!IsPostBack)
{
drop.SelectedIndex = 5;
//yourcode
}
}
Through this your problem will be solved

click on button in asp.net

I am creating a button dynamically and place it in a placeholder as below
<asp:Button ID="generateTableSchema" runat="server" Text="Generate Table" OnClick="generate_Click" />
protected void generate_Click(object sender, EventArgs e)
{
Button button = new Button();
button.Text = "Generate Table";
button.ID = "generateTable";
button.OnClick = hello();
PlaceHolder1.Controls.Add(button);
}
but onclick event is not firing.
this is the error i am getting
System.Web.UI.WebControls.Button.OnClick(System.EventArgs)' is inaccessible due to its protection level
hello is as below...
public void hello()
{
Label1.Text = "heellllllllllo";
}
What's wrong here????
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
}
else
{
button.Click += ButtonClick;
}
}
#Daren u mean like this...
Because you are adding the button programmatic ally you have to add the event handler.
So this would work..
EDIT
Wrapped the button INSIDE Page_Load
protected void Page_Load(object sender, EventArgs e)
{
Button button = new Button();
button.Text = "Generate Table";
button.ID = "generateTable";
button.Click += hello; /// THIS is the handler
PlaceHolder1.Controls.Add(button);
}
ButtonClick would be the name of your method.
protected void hello(Object sender, EventArgs e)
{
// ...
}
Also, as you're generating this at runtime you need to makesure this gets called on postbacks too.
The OnClick is a protected method. You should use the event Click.
button.Click += new EventHandler(Click);
public void hello(object sender, EventArgs e)
{
Label1.Text = "heellllllllllo";
}
By the way, make sure you create and add the control in every postback, otherwise the event won't work.
Change button.OnClick = hello(); to:
button.Click += new EventHandler(hello);
And change the definition for hello() to:
protected void hello(object sender, EventArgs e)
{
Label1.Text = "heeellllllo";
}
The event is called Click. You need to add the event handler with the correct signature:
button.Click += new EventHandler(hello);
and the signature is:
protected void hello(Object sender, EventArgs e)
{
// ...
}
How to: Add an Event Handler Using Code
Note that you need to recreate dynamical controls on every postback.
You assigning the result of executing hello().
Try assigning:
button.OnClick = hello;
-- edit--
apparantly, that does not clarify your error.
Add the handler to the event handler in stead:
button.Click += hello;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
}
else if(Page.IsPostBack && Label11.Text=="yes")
{
Button button = new Button();
button.Text = "Generate Table";
button.ID = "generateTable";
button.Click += ButtonClick;
PlaceHolder1.Controls.Add(button);
}
}
setting
Label11.Text = "yes";
in generate_click.
protected void ButtonClick(object sender, EventArgs e)
{
Label1.Text = "heeellllllo";
}

gridview on select row event

How do I do a grdiview on select row event? On the source page, I added
OnSelectedIndexChanged="grdTanks_OnSelectRow"
in the code behind, I put the function
protected void grdTanks_OnSelectRow(Object sender, GridViewCommandEventArgs e)
{
}
When I try to do it that way, I get No overload for grdTanks_OnSelectRow matches delegate 'System.EventHandler'
If I change the GridViewComandEventArgs to EventArgs, then it won't allow me to do
if (e.CommandName == "Select")
anybody know how to do a OnSelectRow event for a gridview? Thanks
I also added this code:
protected void grdTanks_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowIndex != -1)
{
e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.background='#3260a0';;this.style.color='white'";
if (e.Row.RowIndex % 2 == 1)
{
e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';this.style.background='white';this.style.color='black'";
}
else
{
e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';this.style.background='#bEc8bE';this.style.color='black'";
}
e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.grdTanks, "Select$" + Convert.ToString(DataBinder.Eval(e.Row.DataItem, "CargoTankID")));
}
}
}
You could try this:
In the page_load, enter
grdTanks.SelectedIndexChanged +=
and press tab twice. Visual Studio automatically generates the handler for you. The second param would be EventArgs
I think you have to change
protected void grdTanks_OnSelectRow(Object sender, GridViewCommandEventArgs e)
To
protected void grdTanks_SelectedIndexChanged(Object sender, GridViewCommandEventArgs e)
In your code behind
I am not sure what you want to do in this event handler. You are already handling for select event and I think, no need to check again for (e.CommandName == "Select"). (MSDN:The SelectedIndexChanged event is raised when a row's Select button is clicked).
The error said no overload for event and you have to use EventArgs argument.
protected void grdTanks_OnSelectRow(Object sender, EventArgs e)
{
// May be you want like..
// Get the currently selected row using the SelectedRow property.
GridViewRow row = YourGridViewID.SelectedRow;
}

Resources