Which link is clicked in asp.net gridview row - c#

I am adding two links and some plain text in same cell of gridview, I also have gridview_SelectedIndexChanged function which is called when any of the link is clicked and based of the value from grid I am running my db queries. gridview is also created dynamically so can have different number of rows.
Is there a way to know Link1 or Link2 is clicked in gridview_SelectedIndexChanged function?
protected void gridview_SelectedIndexChanged(object sender, EventArgs e)
{
if (Link1.Clicked)
{do this}
elseif (Link2.Clicked)
{do this}
}

You would want to track which link is clicked by using an ASP.NET control
<asp:LinkButton ID="Link1" runat="server" Click="Link1_Click" />
in your html
Then add an event handler in your backend like
public void Link1_Click(object sender, EventArgs e)
{
//add variable marking this link was clicked
link1_clicked = true;
Response.Redirect("Link1Destination.aspx");
}
and do the same for link2
<asp:LinkButton ID="Link2" runat="server" Click="Link2_Click" />
public void Link2_Click(object sender, EventArgs e)
{
//add variable marking this link was clicked
link2_clicked = true;
Response.Redirect("Link2Destination.aspx");
}
Add the boolean variables link1_clicked and link2_clicked to the top of you backend code. Then when you need to check what has been clicked you can filter though your boolean variables to see what is marked true as clicked with a for-loop.
Basically the Event handlers are your if clicked statements.

Related

How do I disable RadButton from server-side?

I have this button in my aspx file.
<telerik:RadButton ID="btnEnable" OnClick="btnEnable_Click" runat="server" ToolTip="enable"
Text="Enable" Enabled="false" Icon-PrimaryIconUrl="~/images/icon.png">
</telerik:RadButton>
I am disabling it on client-side like this :
btnEnable.set_enabled(false);
I want to add server-side code in my aspx.cs. So when item is selected in grid, I want this button to be disabled from server-side.
function looks like this
protected void btnEnable_Click(object sender, EventArgs e)
{
if(someLogic){btnEnable must be disabled}
}
If it helps, this button can be called after item is selected in the grid.
Remember you can add ToggleStates:
MyButton.ToggleType = ButtonToggleType.CheckBox;
MyButton.ButtonType = RadButtonType.ToggleButton;
MyButton.ToggleStates.Add("Selected");
MyButton.ToggleStates.Add("Unselected");
MyButton.Checked= false; //set to unselected

paging in a gridview based on a year field

I am trying to find a way to page through a gridview table by year. In my database I have a "season" field and I want all of the data from 2014 shown on one page, all the data from 2013 on another, etc. This would also require different numbers of rows based on the year.
Here is how I would do it. I wouldn't use the default paging of the gridview to handle this. Instead, I would buttons (or linkbuttons) to imitate the paging so that you control the paging. This way, you will be able to handle the change in year as well as control what is displayed.
The Steps are as follows:
1) Add your gridview to the page with a big page size just to display everything
<asp:GridView ID="dgvRequests" runat="server" AutoGenerateColumns="False" PageSize="9999"> </asp:GridView>
2) Bind the Per Year data to it in the code behind since you are display everthing per year in your page load. As well as store a variable to track the year
public int CurrentYear
{
get {
if(ViewState["currentYear"] != null)
return (int)ViewState["currentYear"];
return 2014; //Default
}
set {
_ViewState["currentYear"]= value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
dgvRequests.DataSource = GetYearData(CurrentYear); //Or Your variable
dgvRequests.DataBind();
}
}
3) Make controls to control the page. I used Link Buttons
<asp:LinkButton ID="lbtnPrevious" runat="server" OnClick="lbtnPrevious_Click"></asp:LinkButton>
<asp:LinkButton ID="lbtnlNext" runat="server" OnClick="lbtnlNext_Click"></asp:LinkButton>
4) On the click event, control the paging of the gridviews via the click event
protected void lbtnlNext_Click(object sender, EventArgs e)
{
dgvRequests.DataSource = GetYearData(++CurrentYear); //Or Your variable
dgvRequests.DataBind();
}
protected void lbtnPrevious_Click(object sender, EventArgs e)
{
dgvRequests.DataSource = GetYearData(--CurrentYear); //Or Your variable
dgvRequests.DataBind();
}
That should allow you to "Page" in essence by using controls instead of the paging feature.
** The GetYearData function is your function in which you grab your data from
** You may also want to display the current year of data your are displaying

Checkboxlist loop is not working

I have a dropdownlist control and a button in asp.net page. The dropdownlist is populated from a method. If I select any item other than the first item, after clicking the button, I lose the selected item in the DDL and it selects the first item and also I am getting the value of the first item only in the button click event. How can I fix the problem?
<asp:DropDownList ID="userDropDown" runat="server" DataTextField="CustomerName" DataValueField="CustomerId">
</asp:DropDownList>
protected void Button1_Click(object sender, EventArgs e)
{
if(!page.isPostBack)
{
userDropDown.DataSource = CC.GetCustomers();
userDropDown.DataBind();
}
}
i think you must have bind userDropDown in Page_Load event without condition
if (!IsPostBack)
Please put dropdown binding part inside if (!IsPostBack) condition then it should work
Please bind dropdownlist values inside the if(!ispostback){} or
after submitting button please bind updated field to dropdownlistname.text
It sounds like you are binding your DropdownList to your datasource at ever request. Instead bind it only if Page.IsPostBack is false like below; (You may not need ObjectDataSource)
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//bind your datasource here (something like below)
userDropDown.DataSource = GetCustomers();
userDropDown.DataBind();
}
}
As soon as DataBind() method is called it will lose posted data of that object and the FirstItem will be selected by default.

Add radio button list items programmatically in asp.net

I have a radio button list whose items I need to add on Page_Load
aspx code
<asp:radioButtonList ID="radio1" runat="server" RepeatLayout="Flow" RepeatDirection="Horizontal">
</asp:radioButtonList>
code behind
protected void Page_Load(object sender, EventArgs e)
{
RadioButtonList radioList = (RadioButtonList)Page.FindControl("radio1");
radioList.Items.Add(new ListItem("Apple", "1"));
}
After the control reaches radioList.Items.Add
I keep getting the Object reference not set to instance of an object
error
What am I doing wrong?
You don't need to to do a FindCOntrol. As you used the runat="server" attributes, just get the reference of your RadioList via its name "radio1"
protected void Page_Load(object sender, EventArgs e)
{
radio1.Items.Add(new ListItem("Apple", "1"));
}
By using
RadioButtonList radioList = (RadioButtonList)Page.FindControl("radio1");
radioList.Items.Add(new ListItem("Apple", "1"));
you are not adding your list on the control on your page, but on an un-instanciated Radiobuttonlist called radioList.
If the page is reachable from the class, use
radio1.Items.Add(new ListItem("Apple", "1"));
you must add !ispostback
if (!IsPostBack)
{
radio1.Items.Add(new ListItem("Apple", "1"));
}
As an alternative to using the < asp: **> tools -
I needed to reuse a radio option which relies on a lot of jQuery integration in the site. (Also wanted to avoid just CSS hiding the content within the html code of the aspx page.)
The radio buttons needed only appear in an 'edit' page depending on security ACU level logic within the codebehind and rendered with currently stored item value data found in the db.
So I used the following:
string RadioOnChk1 = (db.fieldChecked == true) ? "checked='checked'" : "";
string RadioOnChk2 = (db.fieldChecked == false) ? "checked='checked'" : "";
if (ACU > 3)
{
// Create radio buttons with pre-checked
StringBuilder RadioButtns = new StringBuilder(); // Form input values
{
RadioButtns.Append("<p><label><input type=\"radio\" id=\"radiocomm1\" name=\"custmComm\" value=\"1\"");
RadioButtns.Append(RateIncChk1 + "/>Included or </label>");
RadioButtns.Append("<label><input type=\"radio\" id=\"radiocomm2\" name=\"custmComm\" value=\"2\"");
RadioButtns.Append(RateIncChk2 + "/>Excluded</label>");
RadioButtns.Append("</p>");
}
htmlVariable = (RadioButtns.ToString());
}
It works.. Is this a wrong way of going about it?

Change the value of a textbox in a template field from event outside of GridView

I need to be able to change the value of a TextBox(s) in a GridView template field from a TextChanged event. So the user can enter some text in a TextBox outside of the Gridview and then the TextBox(s) in the GridView gets updated to what the user entered.
This is what I need to do:
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
template_text_box1.Text( in template field ) = TextBox1.Text << (TextBox1)( outside of gridview )
}
I have tried FindControl. This needs to happen without using any of the GridView events. I am just stumped. Could someone point me in the right direction? Maybe some JavaScript?
I believe that you would want to define a separate TextBox for the display and do something like the following:
double value1;
private void template textBox1_TextChanged(object sender, TextChangedEventArgs e)
{
if textBox1.Text (Double.TryParse(textBox1.Text, out value1))
{
textBox15 = value1.ToString();
}
}
This way you can make your other TextBox outside the grid and be able to call it and set to the value that is inputted.
On the .Aspx page, in the GridView column template TextBox add a CSS class.
<asp:TextBox ID="TextBox1" runat="server" CssClass="box-to-change" Text=""></asp:TextBox>
Also on the .Aspx page add a JavaScript function that uses jQuery:
<script type="text/javascript">
function updateAllTextboxes(value)
{
$('input.box-to-change').val(value);
}
</script>
In the code-behind add the JavaScript function as a client OnChange event (will not require PostBack).
otherTextBox.Attributes["onchange"] = "updateAllTextboxes(this.value)";

Categories

Resources