OnClick missing in LinkButton controls? - c#

I'm trying to get the ProductID from the Linkbutton I click on, but I cant find an OnClick choice, just the OnClientClick, but it dont works, are there another way to do it?
protected void Page_Load(object sender, EventArgs e)
{
DataTable table = CategoryAccess.GetAllProducts();
for (int i = 0; i < table.Rows.Count; i++)
{
LinkButton lbDelete = new LinkButton();
lbDelete.Text = table.Rows[i]["ProductName"].ToString();
lbDelete.ID = table.Rows[i]["ProductID"].ToString();
lbDelete.CommandArgument = table.Rows[i]["ProductID"].ToString();
lbDelete.OnClientClick = "btnDelete_Click";
phDelete.Controls.Add(lbDelete);
}
}
protected void btnDelete_Click(object sender, EventArgs e)
{
LinkButton _sender = (LinkButton)sender;
string TrixID = _sender.CommandArgument;
DeleteProduct(ProductID);
}

Hmm, I think I understand what you are doing, but going about it in a funny way.
What I usually do i scode the rowcommand event. I add commandargs='<%# Eval("Id") %>' (or something like that) and pass it to the rowcommand event where I then grab e.commandargs and use that value to delete the database row.
here is one of many examples of what I' talking about
http://www.codeproject.com/KB/webforms/GridViewConfirmDelete.aspx
Sorry if this isn't helpful.

Related

Dynamically recreated TextBox Value in Page_PreInit dependent on value of HiddenField

I create some dynamic controls eg: TextBox.
These a created in a ModalPopupExtender and are created after a ButtonClick.
protected void AddGroupBTN_Click(object sender, EventArgs e)
{
GroupMPE.Show();//GroupMPE is a ModalPopupExtender
ScheduleIdHF.Value = 1; //ScheduleIdHF is a HiddenField declared in the .aspx page
CreateControls(ScheduleIdHF.Value);
...
}
private void CreateControls(string ScheduleId)
{
TableRow TR = new TableRow();
TR.ID = "tableRow1";
TableCell TC = new TableCell();
TC.ID = "tableCell1;
TextBox textBox = new TextBox();
textBox.ID = "textBox1";
TC.Controls.Add(textBox);
TR.Cells.Add(TC);
ExampleTable1.Rows.Add(TR);//ExampleTable1 is declared in the .aspx page
}
Then when another button is clicked I want to recreate these controls on Page_PreInit like this.
protected void Page_PreInit(object sender, EventArgs e)
{
if(IsPostBack)
{
if (!string.IsNullOrEmpty(ScheduleIdHF.Value))
{
CreateControls(ScheduleIdHF.Value);
...
However I want the method call to CreateControls to be conditional on and using the value of the HiddenField ScheduleIdHF. The problem is that the HiddenField is null and is not created to after the Page_PreInit event. Does anybody have any solutions to solve this conundrum? Because I want to get the text of the TextBox after postback.
You can easily access the textbox/hiddenfield value using the following code. This is just using basic web programming idea that anything posted to server is available in Request object.
Code-behind to access control values in PreInit event
protected void Page_PreInit(object sender, EventArgs e)
{
if(Page.IsPostBack) {
var x = Request[TextBox1.UniqueID];
var y = Request[ScheduleIdHF.UniqueID];
//use values of x and/or y to implement your logic
if(y != null && y == "somevalue") {
//your custom logic goes here
}
}
}
Textbox markup
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:HiddenField ID="ScheduleIdHF" runat="server" Value="1010"/>
Use a Session variable to store the value of the HiddenField and then retrieve it in the PreInit event, like:
protected void Page_PreInit(object sender, EventArgs e)
{
if (IsPostBack)
{
string ScheduleIdHF = string.Empty;
if (Session["ScheduleIdHF"] != null)
{
ScheduleIdHF = Session["ScheduleIdHF"].ToString();
CreateControls(ScheduleIdHF);
...
}
}
}

Why doesn't data load on page_load?

I have a button whose text (a counter on datatable) should be changed when I click Update or Add button.
But it doesn't. It only does when I refresh the page only, why ?
Button are within UpdatePanel.
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = ShowLastHearingDates();
if (dt.Rows.Count > 0)
{
btnShowLasthearingDates.Text = dt.Rows.Count.ToString();
}
update:
protected void btnupdate_click(object sender, Eventargs e)
{
if (MngCaseHearings.UpdateCaseANDHearingDetails(CaseNo, CaseTitle))
{
btnUpdate.Visible = false;
btnAddCaseAndHearingDetails.Visible = true;
}
}
Problem is that the page load event happens before your update occurs. You can put the code in the page prerender event which will be hit after the page load and control event
try to put UpdateMode to "always"
or put all related control in the samne panal
or put AsyncPostBackTrigger
this urls my help
https://msdn.microsoft.com/en-us/library/bb386454.aspx
http://code.runnable.com/UhmIdrdIZy9aAATR/how-to-use-updatepanel-in-asp-net

The RowDataBound events of the gridview is not fired after turning to the next page

I use a gridview to display the search result. After clicking search button, the gridview will show page 1, but when I click page 2 link, the gridview disappeared and it was back when I click search button again and show page 2's content.
here is my code
<asp:GridView ID="searchresult" runat="server" AutoGenerateColumns="true" AllowPaging="true" OnRowDataBound="searchresult_RowDataBound" OnPageIndexChanging="searchresult_PageIndexChanging"
HeaderStyle-BackColor="#f9e4d0"
HeaderStyle-Height="20px"
Font-Size="11px"
AlternatingRowStyle-BackColor="#cfdfef"
Width="800px" style="text-align:left">
</asp:GridView>
and code behind
protected void search_Click(object sender, EventArgs e)
{
List<someclass> totalResult = new List<someclass>();
..... //some code to generate the datasource
searchresult.DataSource = totalResult;
searchresult.AllowPaging = true;
searchresult.DataBind();
}
protected void searchresult_RowDataBound(object sender, GridViewRowEventArgs e)
{
}
protected void searchresult_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
searchresult.PageIndex = e.NewPageIndex;
DataBind();
}
I have no idea why the page 2 won't show up until I click search button again. when I clicked the page 2 link, the page did postback but the RowDataBound event was not fired
You have to give your grid a datasource. It appears you are only doing this on search_Click, so your grid is only going to have data then. Try something like:
protected void search_Click(object sender, EventArgs e)
{
PopGrid();
}
protected void searchresult_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
searchresult.PageIndex = e.NewPageIndex;
PopGrid();
}
protected void PopGrid()
{
List<someclass> totalResult = new List<someclass>();
..... //some code to generate the datasource
searchresult.DataSource = totalResult;
searchresult.AllowPaging = true;
searchresult.DataBind();
}
searchresult_PageIndexChanging event handler will make that functionality work. However I recommend you use a gridview control inside a Panel with vertical scroll bar. My user love it and it is a lot faster moving up and down the gridview without defining any page index changing.
I hope it work for you.

Command Argument not working link button

I have a link button and a set of records. When the link button of a specific record is clicked, i want the id of that record to be passed to the code behind. This is the code that i have used:
<asp:LinkButton ID="Likes" runat="server" OnCommand="LinkButton1_Click"
CommandArgument='<%#Eval("datarow["ID"]") %>' CommandName="Like">
Click</asp:LinkButton>
and in the cs file i have used:
protected void LinkButton1_Click(object sender, CommandEventArgs e)
{
int x = Int32.Parse(e.CommandArgument.ToString());
}
But the command argument is null here. can you please help me?
You would handle the wrong event. I think your LinkButton is inside a another control as #greg84 said, for example, you could handle event ItemDataBound of DataGrid or Repeater .
protected void Control_ItemDataBound(object sender, ControlItemEventArgs e)
{
LinkButton Likes = (LinkButton)e.Item.FindContro("Likes");
// Write code here to handle for like click
// ...
}

How to send a value from a LinkButton to the server?

I have many LinkButton such as:
<asp:LinkButton runat="server" onclick="cmdCancellaComunicazione_Click">X</asp:LinkButton>
they call the same server method, cmdCancellaComunicazione_Click. but I need to distinguish them (passing to the server, for example, a value).
How can I do it? I know there is CommandArgument, but I can't set to it a value such as <%= myValue %>
You can use the sender argument of the event-handler. Cast it to LinkButton:
protected void cmdCancellaComunicazione_Click(Object sender, EventArgs e)
{
LinkButton lbtn = (LinkButton) sender;
}
Then you can use it's CommandName, ID or Text to distinguish.
Your code has no ID specified on your LinkButton which seems odd.
You should be able to assign the CommandArgument server side with:
yourLinkButton.CommandArgument = yourValue;
And then it will be read it server side in your OnClick handler.
protected void cmdCancellaComunicazione_Click(object sender, EventArgs e)
{
LinkButton btn = (LinkButton)sender;
if (btn.CommandArgument.Equals(something here))
{
// do something
}
else
{
// do something else
}
}
Is this being created in a grid or something that is being bound? If so I would implement the OnDataBinding event for the LinkButton like:
<asp:LinkButton ID="yourLinkButton"
runat="server" OnDataBinding="yourLinkButton_DataBinding"
onclick="cmdCancellaComunicazione_Click">X</asp:LinkButton>
Server side code (I try to avoid inline code whenever possible):
protected void protected void lblID_DataBinding(object sender, System.EventArgs e)
{
LinkButton btn = (LinkButton)sender;
btn.CommandArgument = yourValue;
}
Is there something more to your scenario that you have not included in your question?
In your method, you can cast the sender to a LinkButton and inspect the value there.
OnCommand would be a good option here.
protected void ButtonCommand(object sender, CommandEventArgs e)
{
string theCommand = e.CommandArgument.ToString();
}
Just add the OnCommand and CommandArgument to the LinkButton.
<asp:LinkButton id="LinkButton1" Text="The Text" OnCommand="ButtonCommand" CommandArgument="YourInfo" runat="server"></asp:LinkButton>

Categories

Resources