Text property overriden - c#

I have a textbox and a linkbutton within the EditItemTemplate of my ListView:
<asp:TextBox ID="txt_notes" runat="server" Placeholder='<%# Eval("notes") %>'></asp:TextBox>
<asp:LinkButton ID="btn_update" class="btn btn-sm btn-success" OnClick="btn_update_Click" CommandArgument='<%# Eval("carID") %>' runat="server" >Update</asp:LinkButton>
I have this code:
protected void btn_update_Click(object sender, EventArgs e)
{
var btn_update = (LinkButton)sender;
int ID = System.Convert.ToInt32(btn_update.CommandArgument);
TextBox txt_notes = (TextBox)listview.EditItem.FindControl("txt_notes");
string notes = txt_notes.Text;
}
Now when I set a break point at string notes = txt_notes.Text it says the txt_notes.Text has nothing in it even though I have typed something in the textbox, so it seems that it is being overridden by the ItemDataBound or the PageLoad.
Does anyone know how I should overcome this problem?

It looks like you are binding your listview on page load but not checking if it is on a postback. try the below
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
listview.DataSource = your_data_source;
listview.DataBind();
}
}

Related

Checking if the value of a textbox is changed or not

I have a textbox filled at the page load. I want to check the value of the textbox is changed or not in the "update" button press. Any solution? TIA.
Well, you could say use client side javascript.
But, you could also do this:
Say this text box:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="Done - continue" OnClick="Button1_Click" />
And our code could be this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// code here to setup and load controls
TextBox1.Text = "Dog";
ViewState["TextBox1"] = "Dog";
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text != (string)ViewState["TextBox1"])
{
// then text box was changed
}
}

persist ddl values and selected value in between postbacks

I have a save button and a drop down list on a page. Inside the page Load, the drop down list is populated if !Page.PostBack (AutoPostBack=false). So, the first time I load the page, the drop down list is populated. I also have a save method to go with the save button. When this button is clicked, it should do something with the selected value of the drop down list. My problem is that the drop down list has no value (is null) inside the button save method. How would you fix this?
Markup:
MyClass.aspx
<%# Page Language="C#" AutoEventWireup="true" Inherits="MyClass" %>
<asp:Content ID="Content3" ContentPlaceHolderID="MainRegion" runat="server">
<div>
<asp:DropDownList ID="myDdl" runat="server" OnSelectedIndexChanged="myDdlChange" ViewStateMode="Enabled" EnableViewState="true" />
</div>
<br />
<div style="min-width: 300px; max-width: 770px;">
<asp:TextBox id="txtBox" runat="server" TextMode="MultiLine" />
</div>
<div class="buttonContainer">
<span >
<asp:Button ID="btnSave" runat="server" Text="Save" onclick="btnSave_Click" />
</span>
</div>
</asp:Content>
Then, in the code behind:
MyClass.aspx.cs
public class MyClass
{
protected global::System.Web.UI.WebControls.DropDownList myDdl;
protected global::System.Web.UI.WebControls.TextBox txtBox;
protected global::System.Web.UI.WebControls.Button btnSave;
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (this.Page.IsPostBack)
Session["selectedID"] = myDdl.SelectedValue; // my attempt to put the selected value from ddl in a session var, to use it later inside the save method but it didn't work
if (!Page.IsPostBack)
{
//create array1 here
myDdl.Items.Clear();
myDdl.Items.AddRange(array1);
Session["selectedID"] = myDdl.SelectedValue;
myDdlChange(null, null);
this.DataBind();
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
//do something based on myDdl.SelectedValue (which shouldn't be null)
}
protected void myDdlChange(object source, EventArgs e)
{
txtBox.Text = myDdl.SelectedValue;
}
}
}
I think the main problem is you should be using Page_Load instead of OnLoad.
You don't need to use Session to remember the SelectedValue.
Try something like this which works for me...
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
PopulateDropdown();
}
private void PopulateDropdown()
{
myDdl.Items.Clear();
var array1 = new ListItem[3];
array1[0] = new ListItem("item1", "item1");
array1[1] = new ListItem("item2", "item2");
array1[2] = new ListItem("item3", "item3");
myDdl.Items.AddRange(array1);
myDdl.DataBind();
}
protected void btnSave_Click(object sender, EventArgs e)
{
var selectedVal = myDdl.SelectedValue; // putting a breakpoint here shows myDdl.SelectedValue is not null
}
protected void myDdlChange(object sender, EventArgs e)
{
}

ItemList FindControl working for 1 item

I have a LinkButton within my ItemTemplate of my ListView:
<asp:ListView ID="lvNotification" runat="server">
<ItemTemplate>
<asp:LinkButton runat="server" ID="lbReject" OnClick="Reject_Click" CommandArgument='<%# Eval("offerID") %>' Text="Reject" />
</ItemTemplate>
</asp:ListView>
Then in my code I have:
protected void Reject_Click(object sender, EventArgs e)
{
var lbreject = lvNotification.Items[0].FindControl("lbReject") as LinkButton;
string c = lbreject.CommandArgument;
}
The ListView retrieves 4 rows correctly, and I have placed the Eval("offerID") and it shows an offerID for each item in the list, however when I place it as the CommandArgument in the LinkButton and debug it, it shows the value 1 on ever item in the ListView, I am trying to place the offerID in each LinkButton CommandArgument and be able to access it, but I cannnot do so.
What am I doing wrong?
That is because you are always checking the first item (0 index). You can get the LinkButton from the sender and check its command argument.
protected void Reject_Click(object sender, EventArgs e)
{
var lbreject = (LinkButton)sender;
string c = lbreject.CommandArgument;
}
Here actually OnItemCommand event of ListView can be handy. This way you can control all events from the single point:
<asp:ListView ID="lvNotification" OnItemCommand="lvNotification_OnItemCommand" runat="server">
<ItemTemplate>
<asp:LinkButton runat="server" ID="lbReject" CommandName="Reject_Click" CommandArgument='<%# Eval("offerID") %>' Text="Reject" />
</ItemTemplate>
</asp:ListView>
protected void lvNotification_OnItemCommand(object sender, ListViewCommandEventArgs e)
{
if (String.Equals(e.CommandName, "Reject_Click"))//or any other event
{
LinkButton lbreject = (LinkButton) sender;
string c = lbreject.CommandArgument;
}
}

set an extra property for LinkButton in a repeater in ASP.NET

I have a repeater that has a linked button inside it. I want to get some data in linked button click event. how should I set my extra data and get them in click event? (consider that i want to concat some items in my property)
aspx code:
<asp:Repeater ID="rpSliderRest" runat="server">
<ItemTemplate>
<!-- ITEM-->
<div class="span2">
<div class="thumbnail product-item">
<img src='<%# Eval("PrintTemplate_URL").ToString().Replace("~", "../..") %>'>
</div>
<h6><%# Eval("PrintTemplate_Desc") %></h6>
<asp:LinkButton ID="lbtn1" runat="server" class="btn btn-large btn-block" OnClick="LinkButton1_Click"
Prperty='<%# string.Format("{0};{1}",Eval("PrintTemplate_URL").ToString(),Eval("PrintTemplate_ID").ToString()) %>'>Select »</asp:LinkButton>
</div>
<!-- ITEM-->
</ItemTemplate>
</asp:Repeater>
aspx.cs code:
protected void LinkButton1_Click(object sender, EventArgs e)
{
LinkButton lbtn = sender as LinkButton;
string MyProperty=??????????
}
You can use the Attributes collection.
For example:
protected void LinkButton1_Click(object sender, EventArgs e)
{
LinkButton lbtn = sender as LinkButton;
String MyProperty = lbtn.Attributes["PropertyName"];
}
Personally I'd go down the route of using the link button commandArgument property - as that's what it's there for.
So:
<asp:LinkButton ID="lbtn1" runat="server" class="btn btn-large btn-block" OnClick="LinkButton1_Click"
CommandArgument='<%# string.Format("{0};{1}",Eval("PrintTemplate_URL").ToString(),Eval("PrintTemplate_ID").ToString()) %>'>Select »</asp:LinkButton>
Then
protected void LinkButton1_Click(object sender, EventArgs e)
{
LinkButton lbtn = sender as LinkButton;
string MyProperty= lbtn.CommandArgument;
}

failed to call event from gridview linkbutton

basically i wish to set a session folo by button click, but i failed to do so
i failed to call abcde function(), don't know what to set,i did try onRowCommand,onDataBound,OnDataBinding
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
my link button on gridview
<asp:LinkButton ID="lnkname" runat="server" Text='<%#Eval("movieTitle") %>' Width=500 CommandName="cmdLink">
in order to find link button control i did try DataGridItemEventArgs, but it didnt work as well
protected void abcde(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "cmdLink")
{
string path = //some path;
Session["path"] = path;
((LinkButton)e.Item.FindControl("lnkname")).PostBackUrl = "~/somewhere/ + Session["path"].ToString()";
}
}
y i do so is because my next page function is depend on the session
i use gridview RowCommand
the aspx code snippet
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="btnLink" runat="server" CommandName="btnLinkClick"
CommandArgument='<%# Bind("roll") %>' Text="Find Name"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
the code behind snippet
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "btnLinkClick")
{
string path = //some path;
Session["path"] = path;
var button = e.CommandSource as LinkButton;
button.PostBackUrl = path;
}
}
here an interesting thing is if u set a postbackurl then second time you didn't get to server side it goes to your given url
if you simply want to set url, it is quite easy you can make a property url then bind it through button.PostBackUrl. also if u need some value from client side then set eventagument
Ok, I'm not entirely sure what you mean, however in case you're asking how to manipulate controls within the grid on databinding, it goes like this:
protected void grdvResults_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.DataItem != null)
{
((LinkButton)e.Row.FindControl("lnkname")).PostBackUrl = "~/somewhere/" + Session["path"].ToString();
}
}
you need to set CommandArgument in likbutton declaration, so that you can use it for binding. GridViewRowcommandEvents
protected void abcde(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "cmdLink")
{
string path = //some path;
Session["path"] = path;
LinkButton objButton = (LinkButton)e.Item.FindControl("lnkname"); //this is you are missing
objButton.PostBackUrl = "~/somewhere/" + Session["path"].ToString()";
}
}

Categories