Here is my code.
problem: When click om a row och on select the page is refreshing and i dont get the text in the lable17.text.
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = GridView1.SelectedRow;
Label17.Text = row.Cells[2].Text.ToString() ;
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "this.style.cursor='Pointer';this.style.backgroundColor='Yellow'");
}
}
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
GridViewRow row = GridView1.Rows[e.NewSelectedIndex];
Label17.Text = "you selected" + row.Cells[2].Text;
}
Is your GridView in an UpdatePanel? If not the entire Page will postback when you click on the Button. Also, make sure that if you are setting the Text of Label17 in the Page_Load event that you only do it the first time i.e.
public void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
Label17.Text = "Default Text";
}
}
Related
When I delete a GridView row, I want to display JavaScript confirmation dialog box AND run a function.
How do you do that?
Something like that:
protected void GridViewActivities_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton lb = (LinkButton)e.Row.FindControl("LinkButton1");
lb.Attributes.Add("onClick", "return confirm('You are sure?'); + MyFunction()");
}
}
If you want to run your function based on confirmation box result, try this code
protected void GridViewActivities_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton lb = (LinkButton)e.Row.FindControl("LinkButton1");
lb.Attributes.Add("onclick", "var result = confirm('You are sure?'); if(result) { MyFunction(); } return true; ");
}
}
If you want to run your function irrespective of the confirmation box result, try this code:
protected void GridViewActivities_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton lb = (LinkButton)e.Row.FindControl("LinkButton1");
lb.Attributes.Add("onclick", "confirm('You are sure?'); MyFunction(); return true; ");
}
}
I am using bound fields and inspecting cell text in rowdatabound event. When it's not a postback there's something in cell text. But when I cause a postback cell text is blank but it doesn't show blank on the display (e.Row.Cells[2].Text is what I'm inspecting below)
void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[2].Text != tbUserName.Text)
{
LinkButton b = e.Row.Cells[1].Controls[0] as LinkButton;
b.Visible = false;
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
IEnumerable<Task> tasks = _dbc.Tasks.ToList();
GridView1.DataSource = tasks;
GridView1.DataBind();
}
You need to keep track whether page is being rendered for the first time or is being loaded in response to a postback. Currently gridview is binded again and again on every postback.
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
IEnumerable<Task> tasks = _dbc.Tasks.ToList();
GridView1.DataSource = tasks;
GridView1.DataBind();
}
}
This was user error! I had some code in another area modifying the columns unexpectedly, my apologies.
ive two pages, one with a text box and a button, the other with a button a label. What i want to do is to display contents of the textbox on page 1, in the label of the page2 on button click. and then when i click the button to return to page1. preverse whats entered in the textbox on page1. sorry if its confusing. heres my code
page1.aspx
protected void Button1_Click(object sender, EventArgs e)
{
Session["fstName"] = txtBox.Text;
Response.Redirect("Page2.aspx");
}
page2.aspx
protected void Page_Load(object sender, EventArgs e)
{
string a = Session["fstName"].ToString();
lblPage2.Text = a;
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("WebForm1.aspx");
}
Where do you set the value of the text box when returning to WebForm1.aspx? It should be very similar to what you have for the label on Page2.aspx. Something like:
protected void Page_Load(object sender, EventArgs e)
{
string a = Session["fstName"].ToString();
txtBox.Text = a;
}
At worst, you may need to wrap some error checking around it. Maybe something like:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
if (Session["fstName"] != null)
{
string a = Session["fstName"].ToString();
txtBox.Text = a;
}
}
I am stuck on something: I am creating a hyperlink at runtime that has a navigation URL. I need to define its click event so that I can save a few values to the database. I did something like below but without success.
Could you please suggest an alternative?
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) {
if (e.Item is GridDataItem) {
HyperLink link = (HyperLink)gridDataItem["ContentTitle"].Controls[0];
link.ForeColor = System.Drawing.Color.Navy;
link.ToolTip = Common.grdTextCell(gridDataItem["ContentSummaryDescr"].Text);
link.NavigateUrl = "~/SomePath/" + gridDataItem["ContentName"].Text;
link.Target = "_blank";
link.Attributes.Add("onclick", "document.getElementById('" +
dummyBtn.ClientID + "').click();");
}
}
protected void dummyBtn_Click(object sender, EventArgs e) {
}
But the button click event is not firing, it simply navigates to the URL. What to do please?
For a server side event to fire you would need a LinkButton and not a HyperLink
LinkButton has a Click event handler which you can use.
HyperLink only redirects and has no corresponding Click event handler associated for server side code
You want a LinkButton, not a HyperLink.
Here's some sample code to get you started (not tested)
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
LinkButton link = (LinkButton)gridDataItem["ContentTitle"].Controls[0];
link.Click += dummyBtn_Click;
}
}
protected void dummyBtn_Click(object sender, EventArgs e)
{
Response.Write("dummyBtn_Click");
}
You should be using Link Button. Just replace your Hyperlink with LinkButton in your code.It should work.
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) {
if (e.Item is GridDataItem) {
LinkButton link = (LinkButton )gridDataItem["ContentTitle"].Controls[0];
link.ForeColor = System.Drawing.Color.Navy;
link.ToolTip = Common.grdTextCell(gridDataItem["ContentSummaryDescr"].Text);
link.NavigateUrl = "~/SomePath/" + gridDataItem["ContentName"].Text;
link.Target = "_blank";
link.Click += dummyBtn_Click;
}
}
protected void dummyBtn_Click(object sender, EventArgs e) {
}
The problem really is that i do can trough the selects on the gridview get the data trough the id, but then i use the search option i implemented on the page and the gridview shows the ones it gets that match the result but if i press select it will redirect to the page with the wrong id, isntead of getting the id of the one i selected it gets the id of the field that was on the 1st position of the cell.
Here is the code:
protected void Page_Load(object sender, EventArgs e)
{
TeamGest.DBLayer.DBLTeams dbl = new TeamGest.DBLayer.DBLTeams();
GridView1.DataSource = dbl.List();
GridView1.DataBind();
TeamGest.DBLayer.DBLPlayers dbl1 = new TeamGest.DBLayer.DBLPlayers();
GridView2.DataSource = dbl1.List();
GridView2.DataBind();
}
protected void MyMenu_MenuItemClick(object sender, MenuEventArgs e)
{
{
MyMultiView.ActiveViewIndex = Int32.Parse(e.Item.Value);
int i;
for (i = 0; i <= MyMenu.Items.Count - 1; i++)
{
if (i == Convert.ToInt32(e.Item.Value))
{
MyMenu.Items[i].Text = MyMenu.Items[i].Text;
}
else
{
MyMenu.Items[i].Text = MyMenu.Items[i].Text;
}
}
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = GridView1.SelectedRow;
Response.Redirect("DetalhesClube.aspx?Id="+row.Cells[0].Text);
}
protected void Button1_Click1(object sender, EventArgs e)
{
string searchStringTeam = TextBox1.Text;
GetTeamResults(searchStringTeam);
}
protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = GridView2.SelectedRow;
Response.Redirect("DetalhesJogador.aspx?Id=" + row.Cells[0].Text);
}
protected void Button2_Click(object sender, EventArgs e)
{
string searchStringPlayer = TextBox2.Text;
GetPlayerResults(searchStringPlayer);
}
Don't use the cell values. That's what the DataKeys collection is for:
<asp:GridView ID="GridView1" runat="server" DataKeyNames="ID, SomeOtherColumn" ...>
And in code-behind all you need is the row index:
var rowIndex = 0;
var ID = (int)GridView1.Rows[rowIndex]["ID"];