I am using linkbutton to insert data in the asp.net grid view through textbox. But in spite of correct code my button is not performing action.
Here is my ASPX code
<FooterTemplate>
<asp:LinkButton ID="Insertkey" OnClick="Insertkey_Click" runat="server">INSERT</asp:LinkButton>
</FooterTemplate>
Here is my C# code
protected void Insertkey_Click(object sender, EventArgs e)
{
SqlDataSource1.InsertParameters["Title"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("Tbox")).Text;
SqlDataSource1.InsertParameters["Pic"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("Pbox")).Text;
SqlDataSource1.InsertParameters["Content"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("Cbox")).Text;
}
Please help me to solve my problem.
Try using the command event instead of click, then handle the grid's RowCommand event and see what the command name is.
Controls that postback, such as the link button, behave differently when in templated controls such as the gridview, primarily since they can't really be declared in the same way since they're in a template.
Related
I'm having problems adding an event to an ImageButton. I have to create a set of buttons depending on a selected option from a DropDownList. The buttons are created successfully with Database data, but I'm can't attach the OnClick functionality.
The created buttons must share the same Handler.
protected void cmbServ_SelectedIndexChanged(object sender, EventArgs e) {
ServiceID = cmbServ.SelectedValue.ToString();
ServiceName = cmbServ.SelectedItem.ToString();
DataTable dtFirstTab = new DataTable();
dtFirstTab = mySQLConn.getTable(qryCarry); // LOAD DATA FROM DB
foreach (DataRow row in dtFirstTab.Rows) {
FTabBtn = "btn"+(Convert.ToInt32(row["SKU_Credito"])).ToString();
FTabIconURL = row["SKU_Icon"].ToString();
Panel dvFirstTab = new Panel();
dvFirstTab.CssClass = "col-xs-2";
ImageButton IB = new ImageButton();
IB.ID = FTabBtn;
IB.ImageUrl = FTabIconURL;
IB.Click += new ImageClickEventHandler(btnX_click); // <-- PROBLEM
dvFirstTab.Controls.Add(IB);
pnlIcons.Controls.Add(dvFirstTab); // pnlIcons exists in HTML
}
protected void btnX_click(object sender, ImageClickEventArgs e) {
string Obj = ((ImageButton)sender).ClientID;
Cantidad = Convert.ToInt32(Obj.Substring(3, (Obj.Length) - 3));
txtMonto.Text = "$" + Cantidad.ToString();
}
All the buttons appear correctly, but when I click on them they just fire a "submit" action, acting like there's no OnClick assigned.
No CodeBehid example:
If I add this line in HTML (I removed asp tags)
ImageButton ID="btn10" runat="server" ImageUrl="MontoLogo_10ST.png" OnClick="btnX_click"
It does work as intended.
Any ideas? Thanks a lot!
Creating controls dynamically in ASP.NET webforms usually seems easy at the beginning, but problems are very common when it comes to handling events. Even if you assign your event handler correctly, the event handler is not run in a postback until you re-create all the dynamic controls early in page lifecycle. This explains why the sample with the ImageButton on the ASPX works whereas the dynamically created buttons don't.
See this page for details on creating controls dynamically. The most important part is the warning that basically says: if you need to add dynamic controls, better don't.
Usually you can find a way to create all the necessary controls in markup, for instance using a Repeater control. The big advantage of the repeater is that you have control about the markup that is created.
The following sample outlines the necessary steps:
Place a repeater on your aspx-page. If pnlIcons serves no other purpose than being the container for the dynamically created buttons, substitute it by the repeater. Use the Header- and FooterTemplate properties to add the markup that surrounds the ImageButtons (e.g. the div for dvFirstTab).
Think about which data you need to assign to the image button. In your case, the fields "SKU_Credito" and "SKU_Icon" seem to be required.
Place the Image button in the ItemTemplate of the repeater and bind the properties "Id" and "ImageUrl" to the corresponding fields.
Add a Command event handler and bind the CommandArgument property to a value that helps you discern between the image buttons.
In the command event handler, you can use the CommandArgument to discover which button has been clicked. Add the appropriate code that handles the command.
In the SelectedIndexChanged event handler, read the data from the database and bind the repeater to the result. This creates the rows in the repeater with the ImageButtons.
Ok, I've found the reason. The event handling must be assigned in Page_Load event, so I moved everything inside a method and called it from Page_Load, calling it from "SelectedIndexChanged" doesn't work . It's working now.
Thanks!
I am new in ASP.NET and I have a problem..
In my code, btn.Click event does not work for multi buttons what created in tab button control. Can you guys advice me?
Thanks,
<asp:Button Text="TAB MENU" BorderStyle="None" ID="Tab" CssClass="Initial" runat="server" OnClick="Tab_Click" />
<asp:Panel ID="panel1" runat="server" Direction="LeftToRight" HorizontalAlign="Left"></asp:Panel>
protected void Tab_Click(object sender, EventArgs e) {
foreach (...) {
Button btn = new Button();
btn.Click += Button1_Click;
panel1.Controls.Add(btn);
}
}
protected void Button1_Click(object sender, EventArgs e) {
**some code here! but does not work.**
}
Try this
Button btn = new Button();
btn.Click += new RoutedEventHandler(Button1_Click);
panel1.Controls.Add(btn);
Your problem probably lies along the lines of the fact that the button does not exist when the Button1_Click callback is fired... Asp.net webforms is a somewhat leaky abstraction over HTTP, and therefore it's a bit tough to decipher sometimes, but I expect what is happening is that when the postback from your button click occurs, the button doesn't actually EXIST in the control tree, because that button only gets added to the control tree when a tab control is clicked. So because the button does not exist on the postback, webforms doesn't know what to do with the event, so it ignores it...
If your buttons must be created dynamically, consider making them do some javascript to edit some hidden field or something, the value of which you can inspect in the Page_Load method, and then do whatever you want to do functionality-wise with that value.
If they are NOT dynamic, and instead will be the same for each page load (but different for each tab - ie, when the page is loaded, you're drawing some info from a db to decide what buttons to display, but that will not change from postback to postback of the same page), then consider creating the buttons in the Page_Load event instead of inside the event that is raised when the tab is created.
ALTERNATIVELY, since in your comment you suggest that the buttons will be used to open a new window, why not just make the buttons do that client side, ie with javascript window.open commands. Then you don't even need to postback to the server at all...
I've developed a page with postbacks but recently I decided to change it to use callbacks
MasterPage
Page
Repeater
td1
literal to show description
td2
EditControl*** (starts on view mode, on postback to edit mode with save button)
So I have 100 records and for each repeater item I have my control in view mode with the Edit button(linkbutton with href:javascript:void and the callback function Edit('',''))
When I click on Edit, it raise the callback event but at this point, imagine that I click on the 15th control in the repeater but the callback event thinks it's the first control in the repeater, I mean 1st row.
So how can I do to work the accurate control that I really click on it?
Because at the end, I return the control with HtmlTextWriter(RenderControl) and I send it to the client-side receiver data function to render it with innerHtml that works great but just with the first control in the first row.
You need to pass in a CommandArgument to the control so that when the event is raised, you can retrieve the args and know which button was clicked.
Your repeater needs an ItemCommand event:
<asp:Repeater ID="repeater" runat="server" OnItemDataBound="repeater_OnItemDataBound" OnItemCommand="repeater_ItemCommand">
<ItemTemplate>
<asp:LinkButton ID="lnkBtnEdit" runat="server" Text="Edit" />
</ItemTemplate>
</asp:Repeater>
Inside the repeaters ItemDataBound event you would pass the link button a command argument:
protected void repeater_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
{
LinkButton lnkBtnEdit = (LinkButton)e.Item.FindControl("lnkBtnEdit");
lnkBtnEdit.CommandArgument = "foo args";
}
}
Next in the ItemCommand event which is what will be raised when one of the link buttons is clicked, you get the Command Argument which tells you which linkbutton was clicked:
protected void repeater_ItemCommand(object source, RepeaterCommandEventArgs e)
{
//This is how you know which button was clicked
string foo = e.CommandArgument;
}
In reality, you'd probably want to pass in the object ID of the item you're clicking, this would likely redirect to an edit page with the ID as a querystring param. In the edit page you would pull in data about that object by the ID.
I'm trying to change the visibility of a pair of server controls (specifically a LinkButton and Label) that are nested inside the ItemTemplate part of a repeater during the onclick event of the same LinkButton. I've actually already found a solution, but I don't understand why it works and not the way I was trying before. What I was trying originally is as follows:
Nested inside the ItemTemplate of a repeater
<asp:LinkButton ID="lnAdd" CommandArgument='<%#Eval("index") %>' runat="server" Text="Add" OnClick="lnAdd_Click"> </asp:LinkButton>
<asp:Label Visible="false" runat="server" ID="videoAdded" Text="Video Added"></asp:Label>
Then inside of that lnAdd_Click function I have:
Repeater1.FindControl("lnAdd").Visible = false;
Repeater1.FindControl("videoAdded").Visible = true;
where Repeater1 is the id of the repeater these controls are in. This appears to do nothing (though it compiles). I assume this is because for some unknown reason those controls are not found.
What does work is the last answer of the following question: Server controls in an asp.net repeater
The solution appears to be doing something very similar what I've tried to do. Its even using FindControl on a repeater. The only difference (that I can see) is that it obtains the repeater via the sender object. Hmmm, actually, now that I think about it the id of the elements are changed by .Net to some nonesense like ctl00_ContentPlaceHolder1_Repeater1_ctl02_lnAdd when this is actually run, so perhaps that's why its not finding it. But at the same time, in the working solution I just give the normal ID like "lnAdd". Is .Net automatically translating it? Why would it do it for this solution and not for how I was trying to do it originally?
Anyway, I feel like there's something fundamental here that I'm not quite grasping and would appreciate some enlightenment :D
The problem with using Repeater1.FindControl(..)... is that you can't tell which particular Label or LinkButton is being targeted. It's a repeater, so each such item gets assigned a unique identifier as part of the RepeaterItemCollection. In the markup you name it "lnAdd" but that's not what it will be once generated etc.
For reference, here's the code you said worked from the other post:
protected void btnUpdate_OnClick(object sender, EventArgs e)
{
Button b = sender as Button;
if (b != null)
{
RepeaterItem ri = b.Parent as RepeaterItem;
if (ri != null)
{
string name = null;
//Fetch data
TextBox txtName = ri.FindControl("txtName") as TextBox;
I'll explain the reason the above works. First, the button that was clicked is cast from the sender object. We know it's a button since it's the Update button's click handler. Next, we also know that the button appears in a repeater, so it's parent is a RepeaterItem. That's how the ri variable is initialized and cast. With ri available to us, using FindControl on it will now find the given control in that particular RepeaterItem.
You need to call the FindControl on the RepeaterItem that contains the button that was clicked, you do this in the OnItemCommand handler of the repeater, so you get the instance of the RepeaterItem that caused the command.
You have to add a OnItemCommand="ProcessCommands" in the repeater header and then add a ProcessCommand member:
protected void ProcessCommands(object source, RepeaterCommandEventArgs e)
{
LinkButton button = (LinkButton)e.Item.FindControl("lbAdd");
button.Visible = false;
...
}
Hope this helps
I have a page with a repeater in it. I'm writing an event handler so that when the user clicks my WebControl button, the event handler for said button iterates through the items in the repeater using FindControl, then uses some of the controls' values. It seems though, that after the page is loaded, the repeater items populate, but when the button is clicked to post this back, as I iterate through the repeater items, I'm seeing that they're all empty. I don't completely understand the sequencing, but I'm assuming it's because my iteration code is trying to access RepeaterItems that haven't been set yet.
The repeater code is in my OnLoad method. Outside of that, I have my event handler trying to iterate through those items after being clicked. This is essentially what I was trying to do:
protected void MyButton_Click(object sender, EventArgs e)
{
foreach(RepeaterItem item in MyRepeater.Items)
{
MyLabel = (Label)item.FindControl("MyLabel");
}
}
The button is located in the FooterTemplate of the repeater.
<asp:Button runat="server" OnClick="SubmitChecklist_Click" cssclass="BlueSubmit" id="SubmitChecklist" text="Submit" />
Thanks in advance.
Edit: To clarify, the exact error I'm getting is NullReferenceException, when I try to do something, for instance, Response.Write(MyLabel.Text)
Edit: After looking into it more today, this is what I understand to be happening: The repeater is databound on postback. When I then make selections from the generated dropdownlists and hit my button, it posts back again. At this point, the repeater is databound again to it's initial values. So, if I must postback in order to get the users' selections, how can I go about this in the button's eventhandler so that I can get the selected values before that repeater gets databound again?
THe problem, it sounds like, is that you may be binding the data to your repeater on load, but not first checking to make sure it isnt a post back.
example:
You request the page. On Load Fires. You bind the data to the repeater.
You maniupulate the data in the reapter then click your button
The page refreshes with the postback, firing the onload event. The data is rebound to your repeater and all previous data entered has been nullified.
the onclick event is triggered and your code tries to retrieve values that no longer exist.
Make sure your databinding code in your onLoad event is nested within an postback check
if (!Page.IsPostBack)
{
Repeater.DataSource = Datatable;
Repeater.DataBind();
}
I've seen the same thing. I don't understand why, but the data doesn't actually get bound until after all events have fired. I ended up making my data source available at the class level and then indexing.
private DataTable myTable;
protected void Page_Load(object sender, EventArgs e)
{
//populate dataTable
if (!IsPostBack)
{
//databind to repeater
}
}
protected void Submit_Click(object sender, EventArgs e)
{
foreach (RepeaterItem item in repeater1.Items)
{
DataRow row = myTable.Rows[item.ItemIndex];
}
}
Ideal? Certainly not but it works.
Instead of relying on the IsPostBack in my OnLoad, I just seperated all of the different states by putting the databinding of the repeater inside of an event handler after the user selects the first option, rather than relying on the IsPostBack of OnLoad. It was a bit convoluted, but I think I'm doing it the right way this time.