I have an ajaxtabcontainer control in my project which contains a datalist and in datalist, I have set the names as a link button. The datalist that I am binding to a source is containing Names and age. Now I want to use the age as a selected value as we use in dropdownlist selected value? what I want is on clicking the name link button I get its value inside a label? how to do it? what I have done is as follows:
in Default aspx:
<ajaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</ajaxToolkit:ToolkitScriptManager>
<ajaxToolkit:TabContainer ID="TabContainer1" runat="server" ActiveTabIndex="1">
<ajaxToolkit:TabPanel runat="server" HeaderText="TabPanel1" ID="TabPanel1">
<HeaderTemplate>
Names
</HeaderTemplate>
<ContentTemplate>
<asp:DataList ID="DataList1" runat="server" RepeatDirection="Horizontal" RepeatColumns="5" ForeColor="Red"
RepeatLayout="Table" >
<ItemTemplate>
<asp:LinkButton Text = '<%#Eval("names")%>' ID="lnkpro" runat="server" ForeColor="Red" OnClick="btn_Click"></asp:LinkButton>
</ItemTemplate>
</asp:DataList>
</ContentTemplate>
</ajaxToolkit:TabContainer>
in cs code:
protected void btn_Click(object sender, EventArgs e)
{
LinkButton myButton = sender as LinkButton;
if (myButton != null)
{
Label1.Text = myButton.Text;
}
}
protected void datalist1_ItemCommand(object source, DataListCommandEventArgs e)
{
int i;
int index = Convert.ToInt32(e.Item.ItemIndex);
LinkButton lnkid = (LinkButton)e.Item.FindControl("lnkpro");
Label1.Text = lnkid.Text;
i = Convert.ToInt32(lnkid.Text);
}
but they are not working. the btn_click is working but giving the only name not its id. the second datalist_itemcommand is not working? Thank you.
you did not have OnItemCommand="datalist1_ItemCommand" in the aspx page. datalist1_ItemCommand should fire after you set OnItemCommand property.
<asp:DataList ID="DataList1" runat="server" RepeatDirection="Horizontal" RepeatColumns="5" ForeColor="Red" OnItemCommand="datalist1_ItemCommand"
RepeatLayout="Table" DataKeyField="age" >
<ItemTemplate>
<asp:LinkButton Text = '<%#Eval("names")%>' ID="lnkpro" runat="server" ForeColor="Red" OnClick="btn_Click"></asp:LinkButton>
</ItemTemplate>
</asp:DataList>
If you are trying to get the age field value in the datalist1_itemCommand function, you need to set DataKeyFeild= "age". And in the item command function you can do Convert.ToInt32(DataList1.DataKeys[e.Item.ItemIndex]) to get the value. Your item Command function will be
protected void datalist1_ItemCommand(object source, DataListCommandEventArgs e)
{
int age = Convert.ToInt32(DataList1.DataKeys[e.Item.ItemIndex]);
}
Related
I have got a gridview. Each row has a Alternate Names (TextBox) and a hidden button, this button has to get clicked by JQuery on the textbox "onblur" automatically.
<asp:GridView ID="GridView1" CssClass="GridView1" runat="server" AutoGenerateColumns="False" EnableViewState="True">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="checkbox1" runat="server" OnClick="checkboxing(this)" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City Name Id">
<ItemTemplate>
<asp:Label ID="NameId" runat="server" Text='<%# Eval("Name_Id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Alternate Names">
<ItemTemplate>
<asp:TextBox runat="server" ClientIDMode="Static" ID="altTxtNames" Style="display: none" onblur="buttonupdate(this)"></asp:TextBox>
<asp:Button runat="server" ID="TxtButton" ClientIDMode="Static" Style="visibility: hidden; display: none" OnClick="TxtButton_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Whenever "onblur" event occurs in the Alternate Names (textbox)
onblur="buttonupdate(this)"
the following JQuery is executed:
function buttonupdate(alternateTxtNames) {
$('#TxtButton').click();
}
This buttonclick prompts execution of aspx.cs method where the values of edited "Alternate Names" and "City Name Id" from the clicked button grid view row are selected using following code:
protected void TxtButton_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
GridViewRow gvr = (GridViewRow)btn.NamingContainer;
string updatedSNo = (gvr.FindControl("NameId") as Label).Text;
int SNo = Convert.ToInt32(updatedSNo);
string updatedText = (gvr.FindControl("altTxtNames") as TextBox).Text;
}
But everytime the click comes from another row only the first gridview row values are selected which I think is the causality of using ClientIDMode because of which it is not able to distinguish the button click from other rows.
How should I overcome this issue. Can someone kindly guide.
Apart from the logical error of using duplicate id's, if you attach the event handler through jQuery, you would be able to get access to the target of the event.
So in that case, rather than onblur="buttonupdate(this), you could use a jQuery event handler such as this:
Change id to class: (This is necessary to attach the handler to more than one textbox)
<asp:TextBox runat="server" ClientIDMode="Static" class="altTxtNames" Style="display: none" ></asp:TextBox>
New function:
function buttonupdate(row) {
$(row).find('#TxtButton').click();
}
jQuery event handler:
$("#GridView1 .altTxtNames").on("blur", function(event){
var clickedRow = $(event.target).closest("tr");
buttonupdate(clickedRow);
});
The above handler will take the blur event, then look for the closest parent tr, then find the button to click in that row.
I'm not familiar with asp, but the above problem should be solvable through just jQuery.
This is what has worked for me:
Following is the GridView markup:
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="checkbox1" runat="server" OnClick="checkboxing(this)" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Alternate Names">
<ItemTemplate>
<asp:Label runat="server" ID="AlternateNames" Text='<%# Eval("alternateNames") %>'></asp:Label>
<asp:TextBox runat="server" ID="altTxtNames" class="altTxtNameClass" Style="display: none"></asp:TextBox>
<asp:Button runat="server" ID="TxtButton" Style="visibility: hidden; display: none" OnClick="TxtButton_Click" />
</ItemTemplate>
</asp:TemplateField>
This is the Jquery code which changes label to textbox on checkbox click:
function checkboxing(chkbox) {
var cellz = $(chkbox).parent();
var lbl = $(cellz).siblings().find('span[id*=AlternateNames]');
var txtbox = $(cellz).siblings().find('input[id*=altTxtNames]');
if ($(chkbox).is(':checked')) {
lbl.hide();
var str = lbl.text();
txtbox.val(str);
txtbox.show();
}
else {
lbl.show();
txtbox.hide();
}
}
This is the code which gets triggered on textbox blur:
$(document).ready(function () {
$('.altTxtNameClass').on('blur', function (event) {
var clickedRow = $(event.target).closest('tr');
buttonupdate(clickedRow);
});
});
Below is the code that gets called on from above code:
function buttonupdate(row) {
$(row).find('input[id*=TxtButton]').click();
}
Below is the code that gets triggered once Button is clicked
protected void TxtButton_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
GridViewRow gvr = (GridViewRow)btn.NamingContainer;
string updatedSNo = (gvr.FindControl("NameId") as Label).Text;
int SNo = Convert.ToInt32(updatedSNo);
string updatedText = (gvr.FindControl("altTxtNames") as TextBox).Text;
}
Hope this is of some help to somebody. The syntax of the Jquery on how to implement the idea is provided by Rich so got to thank him for that.
Is there a way to update a textbox without using postback? I'd like to populate the txtDFS field with the value of (100 - txtStocked) after the user enters data in the txtStocked field.
The following code works, but it uses postback so the page refreshes. Is there a way to accomplish this without refreshing the page?
<asp:TextBox ID="txtStocked" runat="server" Width="75px" AutoPostBack="true" OnTextChanged="txtStocked_TextChanged"></asp:TextBox>
<asp:TextBox ID="txtDFS" runat="server" Width="75px"></asp:TextBox>
protected void txtStocked_TextChanged(object sender, EventArgs e)
{
int stocked = Convert.ToInt16(txtStocked.Text);
int dfs = (100 - stocked);
txtDFS.Text = dfs.ToString();
txtDFS.Text = txtStocked.Text;
}
Solved this by using an UpdatePanel. Here is the updated code:
<asp:ScriptManager ID="SCManager" runat="server" />
<asp:UpdatePanel ID="SCPanel" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">
<ContentTemplate>
<asp:TextBox ID="txtStocked" runat="server" Width="75px" autopostback="true" OnTextChanged="Update_Text"></asp:TextBox>
<asp:TextBox ID="txtDFS" runat="server" Width="75px" ReadOnly="True"></asp:TextBox>
</ContentTemplate>
</asp:UpdatePanel>
protected void Update_Text(object sender, EventArgs e)
{
int stocked = Convert.ToInt16(txtStocked.Text);
int dfs = (100 - stocked);
txtDFS.Text = dfs.ToString();
SCPanel.Update();
}
I have a textbox which is inside a updatepanel -> detailsview. When I click insert I basically call a method to update DB. Now I'm trying to save the value typed in the textbox in the textbox so I don't lose it.
Shortly I wanna set the textbox value, with the value that is inserted.
My aspx:
<asp:UpdatePanel runat="server" ID="insert" UpdateMode="Conditional">
<ContentTemplate>
<table>
<tr>
<Fields>
<td class="style1">
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="500px" AutoGenerateRows="False"
DataKeyNames="strPositionId,nFolderId,tmVaRPosition" DataSourceID="ODSManualPosVaR"
OnItemInserted="DetailsView1_ItemInserted" OnItemInserting="DetailsView1_ItemInserting"
DefaultMode="Insert" SkinID="detailsviewSkin" EnableModelValidation="True">
<asp:TemplateField HeaderText="Name" SortExpression="strPositionName">
<InsertItemTemplate>
<asp:TextBox ID="strPositionName" Width="380px" MaxLength="49" runat="server" Text='<%# Bind("strPositionName") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Width="380px" Text='<%# Bind("strPositionName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</asp:DetailsView>
</td>
</Fields>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
My Page_Load:
protected new void Page_Load(object sender, EventArgs e)
{
base.Page_Load(sender, e);
strSystemId = Request["nSystemId"];
CheckPermission(strSystemId);
if (!IsPostBack || !PageHeader1.SystemId.Equals(strSystemId))
{
RefreshGrid();
DetailsView1.DataBind();
}
}
When click insert:
protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
{
UpdateDB();
//Trying to store the textbox value in a session variable
Session["Test"] = ((TextBox)DetailsView1.FindControl("strPositionName")).Text;
KeepValues();
}
KeepValues:
private void KeepValues()
{
var txtName = (TextBox)DetailsView1.FindControl("strPositionName");
var name = Session["Test"].ToString();
txtName.Text = name;
}
When I debug it stops at KeepValues and the Session variable is working. But I still can't set the textbox.text to it.
Why does this not work? Is it because of a postback? All I want is the value inside strPositionName to be stored in a variable (working) and then set as textbox.text
I am using datalist to show product id, name and a textbox("Qty") allow user to input order Qty. I got System.NullReferenceException: Object reference not set to an instance of an object error when user click an item to order. My datasource provides only 2 columns (product id and name). I added a textbox("Qty") and a button to the datalist. I can not get the value from the textbox("Qty") to submit. Could it be my datasource does not contain the "Qty" column thus FindControl alway return null value? How do I fix the problem? Thanks. Here is my code:
<asp:DataList ID="DataList1" runat="server" CellPadding="10" DataKeyField="product_id" DataSourceID="SqlDataSource1" RepeatColumns="2">
<ItemTemplate>
<asp:Label ID="product_id" runat="server"
Text='<%# Eval("product_id") %>' /><br/>
<asp:Label ID="product_name" runat="server"
Text='<%# Eval("product_name") %>' />
<br />
<asp:TextBox ID="Qty" runat="server"></asp:TextBox>
<asp:Button ID="ButtonAddToCart" runat="server" Text="Add to Cart" CommandName="addtocart2" OnCommand="DataList1_ItemCommand"
/>
</ItemTemplate>
</asp:DataList>
Here is the code for the button:
public void DataList1_ItemCommand(object source, System.Web.UI.WebControls.CommandEventArgs e)
{
var qtytxtbox = DataList1.FindControl("Qty") as TextBox;
// qtytxtbox always return null, why?
}
Your handler doesn't look correct. you should use DataListCommandEventArgs as second parameter. so try something like this
Markup:
<asp:DataList ID="DataList1" runat="server" OnItemCommand="DataList1_ItemCommand" vCellPadding="10" DataKeyField="product_id" DataSourceID="SqlDataSource1" RepeatColumns="2">
Then Add command name in button
<asp:Button ID="ButtonAddToCart" runat="server" Text="Add to Cart" CommandName="addtocart2" />
and code behind
public void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if(e.CommandName.Equals("addtocart2")
{
TextBox qtytxtbox = (TextBox)(e.Item.FindControl("Qty"));
}
}
i have a datalist and i set DataKeyField
<asp:DataList ID="lstItems" runat="server" RepeatDirection="Horizontal" DataKeyField="itemid" >
<ItemTemplate>
//contents in the datalist here
</ItemTemplate>
</asp:DataList>
i also have a imagebutton say imgADD in the page.My question is how can i get the value of datakey in imgADD click on server side
<asp:DataList ID="DataList_projects" runat="server" RepeatColumns="1" DataKeyNames="ID"
RepeatDirection="Horizontal" onitemcommand="DataList_projects_ItemCommand" >
<HeaderStyle ForeColor="Red" />
<ItemTemplate>
<hr style="color:" />
<asp:Label ID="ltlproject" ForeColor="#cc0000" runat="server" text="PROJECT:"></asp:Label> <b><u><asp:Label ID="lblProject" runat="server" Text='<%# Eval("TITLE")%>' ></asp:Label></u></b> <asp:LinkButton ID="lnkEdit" ForeColor="Red" CommandName="Edit" runat="server">Edit</asp:LinkButton><br />
</ItemTemplate>
</asp:DataList>
in your code behind
protected void DataList_projects_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "Edit")
{
string strId = DataList_projects.DataKeys[e.Item.ItemIndex].ToString();
}
}
i think this will help you....
ASPX code:
//In the DataList ItemTemplate:
..
..
<asp:ImageButton ID="imgADD" runat="server" ImageUrl="uri" ToolTip="Add"
CommandName="Add" />
Code-behind:
protected void DataList1_ItemCommand(object source,
DataListCommandEventArgs e)
{
if(e.CommandName == "Add")
{
string keyID;
// Get the index of the row from which this event was raised.
int idx = e.Item.ItemIndex;
// Get the DataKey with the same index.
object thisKey = DataList1.DataKeys(idx);
if(thisKey != null)
{
keyID = thisKey.ToString();
// do something here
}
}
}