Adding/using Attributes for ASP.Net controls - asp.net

I was wondering where can we get a list of attributes that we can add to an ASP.NET control.
An example is the following block of code from :
protected void Grid1_OnItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
((CheckBox)item["TestColumn"].Controls[0]).Attributes.Add("onClick","javascript:hi();");
}
}
The following code couldn't work which I figure could be due to 'onClick' not applicable to Checkbox ASP.NET control.
Hence, is there a place we can refer to for attributes where we can add to each ASP.NET control? And also for the following code:
Attributes["click"] = abc();
Thanks.

Attributes must be added on RowCreated event of GridView. Please check below.
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs
e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Dummy Code
Button btnButton =
(Button)e.Row.Cells[4].FindControl("btnButton");
btnButton.Attributes.Add("onmouseover",
"this.className='actionH'");
}
}

Related

Add tooltip to only some cells of Grid View

I need to add a tooltip to a TemplateField in a GridView, but only to Row that meets some condition. How can I achieve that?
protected void GridView1_DataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// check for condition
if (e.Row.Cells[0].Text.Contains("sometext"))
{
e.Row.ToolTip = "Text to be shown on mouseover of the row";
}
}
}
Use the row databound which will hit each row as it is created. Then inside use .findcontrol to get whatever control it is that you are adding a tooltip to.
Then assign the tooltip.
Wrap this in whatever condition you want to use.
If you post your current code then I may be able to help further.
try this,
This event through you can check your all gridview rows.
gridview row event Name is =OnRowDataBound : "GridView1_RowDataBound"
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Add Your Condition here
}
}

How to use DataBinding event in DataList

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)
{
}

How to know the previous event handled in asp.net

on page load i have shown data from database onto a gridview(with edit and delete option). I also have a search button on the page. when i click on search the searched data should be visible in the gridview, which is working fine. But when i click on delete link after search it does not take the searched row. The page postsback after clicking on delete. I need to check the event performed prior to event performed on gridview. How to do that?? I hope that the issue is clear.. Any help is appreciated. Thanks.
Page_Load occurs before RowCommand, are you only binding grid if(!IsPostBack) ?
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
BindGrid();
}
}
protected void SearchButton_Click(object sender, EventArgs e)
{
BindGrid(searchText.Text);
}
protected void GridView_RowCommand(object sender, GridViewRowEventArgs e)
{
switch(e.CommandName)
{
case "Delete":
DeleteRow(e.Row.CommandArgument);
break;
}
}
private void BindGrid()
{
//GridView binding business logic.
}
private void BindGrid(String searchTerm)
{
//GridView binding logic with searchTerm.
}
private void DeleteRow(String commandArg)
{
//Convert command arg to ID or whatever and delete data.
}

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;
}

how to display the mark sheet on the next page using ASP.NET?

i want to creat a web page that uses radiobuttons in a multiple choice quetions, but i want to do is to use a submit button to return the results to the next page with the mark sheet, so can you please help me with that. how to display the mark sheet on the next page using ASP.NET
Use Session state.
For example,
protected void Button1_Click(object sender, EventArgs e)
{
Session["key"] = TextBox1.Text;
Response.Redirect("marksheet.aspx");
}
Code in Page_Load of marksheet.aspx,
protected void Page_Load(object sender, EventArgs e)
{
if (Session["key"] != null)
{
object val = Session["key"];
...
}
}

Resources