I have a LinkButton which fires an OnClick event to update some Labels; however, after the first firing of OnClick, it won't fire again when I click another (or the same) LinkButton which runs the same OnClick event. (It's a list of people, each a LinkButton, and clicking on one brings up their details)
If I leave the page a few minutes, it will work again, almost as if whatever was preventing OnClick firing timed-out. Of course, this won't be any use to the users!
This is my ASP.NET code for the LinkButtons (encapsulated in a DataList):
<asp:DataList ID="DataList1" runat="server" DataKeyField="AdmissionNumber">
<ItemTemplate>
<asp:CheckBox ID="CheckBox" runat="server" OnCheckedChanged="Checkbox_CheckedChanged" ViewState="true"/>
<asp:LinkButton ID="LinkButton" runat="server" OnClick="LinkButton_OnClick">
<asp:HiddenField ID="hfAdmissionNumber" Value='<%# Eval("AdmissionNumber") %>' runat="server"/>
<asp:Label ID="CalledLabel" runat="server" Text='<%# Eval("Called") %>' />
<asp:Label ID="SurnameLabel" runat="server" Text='<%# Eval("Surname") %>' />
</asp:LinkButton><br /><br />
</ItemTemplate>
</asp:DataList>
In the Page_Load event in the C# code behind, the following populates the DataList:
connection.Open();
SqlDataReader reader = command.ExecuteReader();
DataList1.DataSource = reader;
DataList1.DataBind();
connection.Close();
And this is the OnClick event:
protected void LinkButton_OnClick(object sender, EventArgs e)
{
LinkButton l = (LinkButton)sender;
HiddenField hfv = (HiddenField)l.Parent.FindControl("hfAdmissionNumber");
using (SqlConnection connection = new SqlConnection("Data Source=pastonmis01\\inform;Initial Catalog=2009;Integrated Security=True"))
{
using (SqlCommand command = new SqlCommand("xProcSportNotParticipantDetails",connection))
{
connection.Open( );
command.CommandType = System.Data.CommandType.StoredProcedure;
command.Parameters.AddWithValue("#AdmissionNumber", Int32.Parse(hfv.Value));
SqlDataReader reader = command.ExecuteReader();
reader.Read();
lTitle.Text = reader.GetValue(0).ToString();
lSurname.Text = reader.GetValue(1).ToString();
lForename.Text = reader.GetValue(2).ToString();
lCalled.Text = reader.GetValue(3).ToString();
lDoB.Text = reader.GetValue(4).ToString().Substring(0,10);
lSex.Text = reader.GetValue(5).ToString();
reader.Close();
}
connection.Close();
}
}
All the connections work, the data is retrieved, etc, so everything except the OnClick firing works. I've done a search of the internet and found this seems to have been a long-standing problem since the first ASP.NET, but there is no solution for ASP.NET 3.5. Does anyone know what causes this, or where I might be going wrong?
Try giving OnClientClick="Page_BlockSubmit=false" for your link button
Related
I have a textbox and a label in ASP.NET and i want to automatically update the label with a value from database that is fetched using the content entered in textbox. This should be done dynamically when the text is changed in textbox and if a match if found in database, the corresponding value should be displayed in the label field (without page refresh). In this case I will enter employee ID in textbox and employee name should be displayed in label. I am using the following code,
<asp:ScriptManager ID="script_manager" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="update_name" runat="server">
<ContentTemplate>
<asp:TextBox ID="text_empID" TextMode="Number" MaxLength="6" AutoPostBack="true" OnTextChanged="text_empID_TextChanged" runat="server"></asp:TextBox>
<asp:Label ID="label_empName" runat="server"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
The code behind is as follows,
protected void text_empID_TextChanged(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("select EmployeeName from EmployeeTable where EmployeeID=#id", con);
cmd.Parameters.AddWithValue("#id", text_empID.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
label_empName.Text = dt.Rows[0]["EmployeeName"].ToString();
}
}
But this is not working. Also after entering a value in textbox and if i click outside the box, the text inside disappears. Is there any other way to do this? or am i doing something wrong here?
I would suggest that you should add a button because the text_changed event only fires when the text-box blurs (loses focus).
That's a very weird behaviour and most users are not used to it and will not expect it.
<ContentTemplate>
<asp:TextBox ID="text_empID" TextMode="Number" MaxLength="6" runat="server></asp:TextBox>
<asp:button id="btnSearch" runat="server" OnClick="text_empID_TextChanged" />
<asp:Label ID="label_empName" runat="server"></asp:Label>
</ContentTemplate>
In any case, have you added a breakpoint? does the event fire? do you get any data back in the DataTable?
EDIT
After your last comment I am convinced that you are clearing the contents of the label on page load.
please, make sure that you only do that in the following context:
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
label_empName.Text = "";
}
}
I'm displaying a datatable that selects a number of elements from the database (IncomingsId, Type, Cost, Frequency) and I'm not sure how to delete a row when a button is clicked.
I've tried many solutions so far but nothing is working.
Here is the button I have within my Grid view
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:button id="DeleteRowButton" text="Delete Record" onclick="DeleteRowButton_Click" runat="server"/>
</ItemTemplate>
</asp:TemplateField>
And here is the code behind this page were I am creating the datatable.
SqlConnection con;
public _Default()
{
con = new SqlConnection(#"MySQLConnection");
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DisplayRecord();
}
}
public DataTable DisplayRecord()
{
string userId = (HttpContext.Current.User.Identity.GetUserId());
SqlDataAdapter Adp = new SqlDataAdapter("select * from Incomings where AspNetUsersId = '" + userId +"'", con);
DataTable Dt = new DataTable();
Dt.AcceptChanges();
Adp.Fill(Dt);
grid1.DataSource = Dt;
grid1.DataBind();
return Dt;
}
public void DeleteRowButton_Click(object sender, EventArgs e)
{
}
Cheers in advance for any help. I'm sure it's a simple resolution
You need a way for your code to know which ID to delete. Here is how I would normally do it:
Replace your button with an ItemTemplate, and add the button in here instead:
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button Text="Delete" runat="server" CommandArgument='<%# Eval("userId") %>' CommandName="Delete" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
When you have the button this way, you now have access to a CommandArgument and CommandName attributes. Notice the argument I am passing is Eval("userId"), the command name (as you will see later) is used to recognize what action you want to execute (this could be anything, is just a name).
Replace the CommandArgument with whatever value(s) you want to pass to the server, using the name that came from the database/datasource (I am guessing it would be userId).
Then, to capture this you need to implement the RowCommand event of the GridView, and intercept the correct CommandName:
public void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
string userId = e.CommandArgument.ToString();
//do something with the id
}
}
Here you have to make sure the CommandName in your button, matches to whatever action you want to execute in the RowCommand. This should do the trick.
Don't forget to bind the event to your GridView:
<asp:GridView OnRowCommand="GridView1_RowCommand" ID="GridView1" runat="server">
...
</asp:GridView>
just use this for the front-end code
<asp:GridView ID="grid1" OnRowDeleting="OnRowDeleting" DataKeyNames="deleteR" runat="server" AutoGenerateColumns="False" CellPadding="4" GridLines="None" style="width:100%" >
<columns>
<asp:TemplateField HeaderText="Delete Row">
<ItemTemplate>
<asp:Button ID="btnDelete" runat="server" class="btn btn-primary" Text="Delete" CommandName="Delete" OnRowDataBound="OnRowDataBound" />
</ItemTemplate>
</asp:TemplateField>
And have this in behind it:
protected void OnRowDeleting(object sender, GridViewDeleteEventArgs e)
{
int deleteR= Convert.ToInt32(grid1.DataKeys[e.RowIndex].Values[0]);
//have a sql statement here that add's the parameter ID for the row you want to delete, something like
SqlCommand com = new SqlCommand ("Delete FROM yourdatabase Where yourID = #yourID"
com.Parameters.AddWithValue("#yourID", yourID)
}
What you're looking for is indeed a simple solution.
You can use a foreach loop to search all DataRows within the DataTable for some kind of ID.Here's an example of what I mean:
String s = "/* IncomingsId for what you want to delete */";
foreach (DataRow r in dt.Rows) {
if (r["IncomingsId"].ToString().Equals(s)) {
dt.Rows.Remove(r);
}
}
Just a quick update for anyone that's helped me, thanks for your advice.
Asp.Net has it's own built in AutoGenerateDeleteButton and I set that to true and ran a bit of code behind it.
A simple solution that really shouldn't have taken me all day to complete!
I have a repeater that I populate from a database:
using (SqlConnection conn = new SqlConnection(connString))
{
SqlCommand cmd = new SqlCommand(#"SELECT CommunityName, CID, Budget FROM Donation WHERE Year = year(getdate()) ORDER BY CommunityName", conn);
conn.Open();
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataSet myDataSet = new DataSet();
adp.Fill(myDataSet);
myRep.ItemDataBound += new RepeaterItemEventHandler(myRep_ItemDataBound);
myRep.DataSource = myDataSet;
myRep.DataBind();
}
void myRep_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
var textbox = e.Item.FindControl("community");
textbox.ClientIDMode = ClientIDMode.Static;
textbox.ID = "community" + (e.Item.ItemIndex + 1);
}
Repeater:
<asp:UpdatePanel ID="UpdatePanel" runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:Repeater ID="myRep" runat="server">
<ItemTemplate>
<div class="form-group">
<asp:Label ID='thisLbl' runat="server" Text='<%# Eval("CommunityName") %>' />
<asp:TextBox runat="server" ID="community" Text='<%# Eval("Budget") %>' CssClass="form-control" />
</div>
</ItemTemplate>
</asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>
This creates 6 textboxes with labels and values, now my question is how do I detect which of these boxes belongs to the record it was initially pulled from in the database? I want to be able to modify the value in these boxes and hit a button to save them back to the database but I can't seem to wrap my head around getting them to the proper records.
Should I set the ID of the textbox to something I can parse through and match with the proper record? In the ItemDataBound?
You have to put a hidden field inside the repeater item template that takes the value from budget, and another hidden field to keep the CID value that has to be read in the post back request. Of course you need also a button and its click event handler.
<asp:Repeater ID="myRep" runat="server">
<ItemTemplate>
<div class="form-group">
<asp:Label ID='thisLbl' runat="server" Text='<%# Eval("CommunityName") %>' />
<asp:TextBox runat="server" ID="txtBudget" Text='<%# Eval("Budget") %>' CssClass="form-control" />
<asp:HiddenField runat="server" ID="hdOriginalBudget" Value='<%# Eval("Budget") %>' />
<asp:HiddenField runat="server" ID="hdCID" Value='<%# Eval("CID") %>' />
</div>
</ItemTemplate>
</asp:Repeater>
<br />
<asp:Button ID="btn" runat="server" OnClick="btn_Click" />
In your code behind you need to loop inside the repeater to check whether the text box has been changed by comparing its value to the hidden field. After you save the budget value in the database you need to realign the hidden field value to the the new value entered by the user, otherwise you will always save that value after each post back:
protected void btn_Click(object sender, EventArgs e)
{
foreach (RepeaterItem item in myRep.Items)
{
var txtBudget = item.FindControl("txtBudget") as TextBox;
var hdOriginalBudget = item.FindControl("hdOriginalBudget") as HiddenField;
var hdCID = item.FindControl("hdCID") as HiddenField;
if (txtBudget.Text != hdOriginalBudget.Value)
{
//If you enter here means the user changed the value of the text box
using (SqlConnection conn = new SqlConnection(connString))
{
SqlCommand cmd = new SqlCommand(#"UPDATE Donation SET Budget = #Budget WHERE CID = #CID", conn);
cmd.Parameters.Add(new SqlParameter("#Budget", int.Parse(txtBudget.Text)));
cmd.Parameters.Add(new SqlParameter("#CID", int.Parse(hdCID.Value)));
conn.Open();
cmd.ExecuteNonQuery();
}
//After you write in the database realign the values
hdOriginalBudget.Value = txtBudget.Text;
}
}
}
Take care that my code is missing the most basic validation, so if the user writes an invalid value in the textbox (for example "yyy") it breaks. So please don't put it in production as it is!
I have a button inside a listview and a label. The label is bound to my database and displays a score. If you press the button I want the score to update on client side. But Since I cant access items in Listview from codebehind I have no idea how to do this. And updatepanel does not work.
My listview bound to a Sqldatasource:
<asp:ListView ID="ListViewSearch" runat="server" DataSourceID="SqlDataSourceWebsite" DataKeyNames="WebsiteID">
My buttons and my label for rating:
<asp:LinkButton ID="BtnUp" runat="server" CssClass="btn btn-default btn-xs" CommandArgument='<%# Eval("testId").ToString() %>' OnClick="up_click"></asp:LinkButton>
<asp:LinkButton ID="BtnDown" runat="server" CssClass="btn btn-default btn-xs" CommandArgument='<%# Eval("testId").ToString() %>' OnCommand="down_click"></asp:LinkButton>
<asp:Label ID="LabelRating" runat="server" Text=' <%# Eval("rating") %>'></asp:Label>
upClick() function:
protected void up_click(object sender, EventArgs e)
{
LinkButton btn = (LinkButton)sender;
String webID = btn.CommandArgument.ToString();
int ID = Convert.ToInt32(webID);
click(ID, 1);
}
Click() function:
public void click(int webID, int vote)
{
using (SqlConnection con = new SqlConnection(strCon))
{
using (SqlCommand cmd = con.CreateCommand())
{
cmd.CommandText = "UPDATE [Test] SET [rating]+=#vote WHERE [testId]=#testID";
cmd.Parameters.AddWithValue("#webID", webID);
cmd.Parameters.AddWithValue("#vote", vote);
con.Open();
cmd.ExecuteNonQuery();
}
con.Close();
}
}
The code updates the rating as it is supposed to, but to see the change I have to refresh the page. So the questions is; How do I update a value inside a listview by clicking a button inside that same lisview?
(If this is not possible, please suggest a different way that I can display results from a search in a list that offers the same customization.)
I'm more into ASP MVC however maybe my tip will help you.
The way I did these behaviors was to bind javascript function to the button and using jQuery I've send 'GET' request to the specific action(method) on the server for example: www.myexample.com/getData?tableid=24 which responds with json containing needed data.
Hope this will direct you to proper solution.
Cheers,
Marek
I am unable to guess the issue here , as the event of grid item command is not executing .I also changes the pageevent validation state but of no use.
I am pasting .aspx code as well as
The grid is binded perfectly
<telerik:RadGrid ID="frds" runat="server" OnItemCommand="go_frd" AutoGenerateColumns="false" >
<MasterTableView>
<Columns>
<telerik:GridTemplateColumn>
<ItemTemplate>
<asp:Button ID="bt" runat="server" CommandArgument='<%#Eval("frd_ID") %>' Text="test" />
</ItemTemplate>
</telerik:GridTemplateColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
The event is this
protected void go_frd(object o, GridCommandEventArgs e)
{
if (e.CommandName == "frd_go")
{
Response.Redirect("Profiling.aspx?uid=" + e.CommandArgument);
}
if (e.CommandName == "add_frd")
{
db_accessDataContext db = new db_accessDataContext();
Frd_request req = new Frd_request();
db.AddFriend(Int64.Parse(cur_mem_id), Int64.Parse(e.CommandArgument.ToString()));
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("dbo.addFriend", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(#"memID", Int64.Parse(cur_mem_id));
cmd.Parameters.Add(#"frdID", Int64.Parse(e.CommandArgument.ToString()));
try
{
con.Open();
cmd.ExecuteNonQuery();
}
catch (Exception ex) { }
}
}
I tried many approaches but unable to fire the event for button in grid
I checked it by putting a break point as well ,the trouble is it don't even start execution of the event
Binding COde
string query = "my query containing the frd_id ,works fine in query builder and it also is shown grid view ";
try { SqlConnection con = new SqlConnection(connectionString);
SqlDataAdapter adapter = new SqlDataAdapter(query, con);
adapter.Fill(d0); con.Close();
} catch (Exception ex) { }
frds.DataSource = d0;
frds.DataBind();
You need to set the CommandName:
<asp:Button ID="bt" runat="server"
CommandName="frd_go"
CommandArgument='<%#Eval("frd_ID") %>' Text="test" />
I had a similar issue today.
In my radgrid I had the code to build the sql and populate the radgrid in the page_load event, but I forgot to add
if (!IsPostBack) { }
around this code, so on every page load the grid was being rebuilt and the onitemcommand method was not working. The page seemed to be was posting back on button click or rowclick, but the event was just not firing. Try adding the if (!isPostback) code around your databinding and you might find it works for you.
You will need to set EnablePostBackOnRowClick property to true for ClientSettings. However this will cause a full post back.
.
.
.
</MasterTableView>
<ClientSettings EnablePostBackOnRowClick="true">
</ClientSettings>
</telerik:RadGrid>
You may want to check this thread on Telerik forums