asp.net C# passing value from gridview to texbox on button click - c#

I have a simple gridview with button for each row, and I want to pass or display the value of user_full_name_ar in a label, I tried doing this using javascript function as shown below but it doesn't show the data it shows null,
gridview code:
<asp:Label ID="Label5" Text='transfer text here' runat ="server" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="user_name" DataSourceID="SqlDataSource1" Height="100px" Width="383px">
<Columns>
<asp:BoundField DataField="user_name" HeaderText="user_name" ReadOnly="True" SortExpression="user_name" />
<asp:BoundField DataField="user_full_name_ar" HeaderText="user_full_name_ar" SortExpression="user_full_name_ar" />
<asp:TemplateField HeaderText="user_full_name_ar" SortExpression="user_full_name_ar">
<ItemTemplate>
<asp:Label ID="Label4" Text='<%# Session["lang"].ToString() == "en"? Eval("user_full_name_en") : Eval("user_full_name_ar") %>' runat ="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="ButtonSearch" runat="server" text="select" OnClientClick ="txt();" CommandArgument ='<%# Bind("user_name") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
javascript function to display value of label4 in gridview to label5 outside gridview :
<script type ="text/javascript" >
function txt() {
var t = document.getElementById("Label4");
document.getElementById("Label5") = t.value;
}
</script>

you can use a CommandField in gridview
<asp:Label ID="Label5" Text='transfer text here' runat ="server" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="user_name" DataSourceID="SqlDataSource1" Height="100px" Width="383px">
<Columns>
<asp:CommandField HeaderText="select" SelectText="select " ShowSelectButton="True">
</asp:CommandField>
<asp:BoundField DataField="user_name" HeaderText="user_name" ReadOnly="True" SortExpression="user_name" />
<asp:BoundField DataField="user_full_name_ar" HeaderText="user_full_name_ar" SortExpression="user_full_name_ar" />
<asp:TemplateField HeaderText="user_full_name_ar" SortExpression="user_full_name_ar">
<ItemTemplate>
<asp:Label ID="Label4" Text='<%# Session["lang"].ToString() == "en"? Eval("user_full_name_en") : Eval("user_full_name_ar") %>' runat ="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="ButtonSearch" runat="server" text="select" OnClientClick ="txt();" CommandArgument ='<%# Bind("user_name") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
after in selectedindexchenge event write code below
Label5.Text = GridView1.SelectedRow.Cells[2].Text;

Related

ASP.NET getting textbox value from inside a DetailsViews with a button is clicked; that textbox data is independent of the binded data

As written in the title, I am trying to get a textbox value from inside a DetailsView. However, the textbox value is not in the content of the Binded data in DetailsView, so when I try to get the text with the OnCommand method, I get an HttpContext error. Secondly, I can't reach the value using its ID, possibly due to it being in DetailsView. And finally, when I put the textbox and button outside of the DetailsView, I get the value, but the button and text shouldn't show up if there is no data to show in DetailsView.
This is my DetailsView code:
<asp:DetailsView ID="dvÜrün" runat="server" GridLines="None" CssClass="table table-borderless" AutoGenerateRows="false" OnCommand="dvÜrün_ItemCommand">
<Fields>
<asp:TemplateField Visible="false">
<ItemTemplate>
<%#Eval("ÜrünID") %> TL
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ÜrünAdı" HeaderText="Adı" />
<asp:BoundField DataField="ÜrünKategori" HeaderText="Kategori" />
<asp:BoundField DataField="ÜrünAçıklama" HeaderText="Açıklama" />
<asp:TemplateField HeaderText="Ücret">
<ItemTemplate>
<%#Eval("ÜrünÜcret") %> TL
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
Adet:
<asp:textbox ID="Adet" runat="server" CssClass="text-center" textmode="SingleLine" type="number" min="1" max="20" Text="1"/>
<asp:Button CssClass="pull-right btn btn-success" Text="Sepete Ekle" runat="server" OnClick="sepeteEkle_Click"/>
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
After posting this, I found the answer immediately. This is my cs code:
public void dvÜrün_ItemCommand(object sender, DetailsViewCommandEventArgs e)
{
if (e.CommandName == "ürünEkle")
{
string adet = ((TextBox)dvÜrün.FindControl("Adet")).Text;
}
}
This is the DetailsView code:
<asp:DetailsView ID="dvÜrün" runat="server" GridLines="None" CssClass="table table-borderless" AutoGenerateRows="false" OnItemCommand="dvÜrün_ItemCommand">
<Fields>
<asp:TemplateField Visible="false">
<ItemTemplate>
<%#Eval("ÜrünID") %> TL
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ÜrünAdı" HeaderText="Adı" />
<asp:BoundField DataField="ÜrünKategori" HeaderText="Kategori" />
<asp:BoundField DataField="ÜrünAçıklama" HeaderText="Açıklama" />
<asp:TemplateField HeaderText="Ücret">
<ItemTemplate>
<%#Eval("ÜrünÜcret") %> TL
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
Adet:
<asp:textbox id="Adet" runat="server" CssClass="text-center" textmode="SingleLine" type="number" min="1" max="20" Text="1"/>
<asp:Button CssClass="pull-right btn btn-success" Text="Sepete Ekle" runat="server" CommandName="ürünEkle"/>
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>

Dynamically add/delete rows from GridView ASPX to CS (source);

I want to create GridView like this in C# Class library:
This is ASPXsource for UI:
<asp:gridview ID="Gridview1" runat="server" ShowFooter="true" AutoGenerateColumns="false" onrowcreated="Gridview1_RowCreated">
<Columns>
<asp:BoundField DataField="RowNumber" HeaderText="Row Number" />
<asp:TemplateField HeaderText="Header 1">
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Header 2">
<ItemTemplate>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Header 3">
<ItemTemplate>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</ItemTemplate>
<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
<asp:Button ID="ButtonAdd" runat="server" Text="Add New Row"
onclick="ButtonAdd_Click" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">Remove</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:gridview>
I want to format ASPX to CS (source), like this format :
{
myGrid = new SPGridView();
myGrid.ShowFooter = true;
myGrid.AutoGenerateColumns = false;
myGrid.ID = "Gridview1";
SPBoundField dest = new SPBoundField();
dest.DataField = "Number";
dest.HeaderText = "Number";
TemplateField headerOne = new TemplateField();
base.CreateChildControls();
}
I have no idea how to do this, please help.

How to disable LinkButton within GridViewclick from Client Side using JavaScript?

I have a GridView, In one column I have LinkButton control. I want to disable click from client side for certain condition on this column. Means for some rows it will not be possible for User to call onclick event and for some rows it is possible.
I want to achieve this from client side using javascript.
Or When User clicks on link, It will notify the User that this action can't be completed for this row.
<asp:GridView Width="100%" ShowHeader="true" ViewStateMode="Enabled" GridLines="Both" CellPadding="10" CellSpacing="5" ID="GridViewMultiplePOSAssociationId" runat="server" AllowSorting="false" AutoGenerateColumns="false" OnRowCommand="RewardGridMultiD_RowCommand"
AllowPaging="true" PageSize="8" OnRowDataBound="grdViewCustomers_OnRowDataBound" PagerSettings-Position="Top" PagerSettings-Mode="NumericFirstLast" PagerSettings-FirstPageText="First" PagerSettings-LastPageText="Last" DataKeyNames="POS Id">
<RowStyle CssClass="table_inner_text" BackColor="WhiteSmoke" BorderColor="CornflowerBlue" Wrap="true" ForeColor="Black" Height="30px" />
<HeaderStyle CssClass="table_head_text" />
<Columns>
<asp:TemplateField ItemStyle-Width="80px">
<ItemTemplate>
<a href="JavaScript:divexpandcollapse('div<%# Eval(" POS Id ") %>');">
<img alt="Details" id="imgdiv<%# Eval("POS Id") %>" src="images/plus.png" />
</a>
<div id="div<%# Eval(" POS Id ") %>" style="display: none;">
<asp:GridView ID="grdViewOrdersOfCustomer" runat="server" AutoGenerateColumns="false" CssClass="ChildGrid">
<RowStyle CssClass="table_inner_text" BackColor="SkyBlue" BorderColor="Black" Wrap="true" ForeColor="White" Height="30px" />
<Columns>
<asp:BoundField ItemStyle-Width="150px" DataField="RULE FILE NAME" HeaderText="RULE FILE NAME" />
<asp:BoundField ItemStyle-Width="150px" DataField="RULE ID" HeaderText="RULE ID" />
<asp:BoundField ItemStyle-Width="150px" DataField="RULE TYPE ID" HeaderText="RULE TYPE ID" />
<asp:BoundField ItemStyle-Width="150px" DataField="START TIME" HeaderText="START TIME" />
<asp:BoundField ItemStyle-Width="150px" DataField="EXPIRY TIME" HeaderText="EXPIRY TIME" />
</Columns>
</asp:GridView>
</div>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width="80px" ItemStyle-Width="80px" ItemStyle-HorizontalAlign="Center" HeaderText="Row Number">
<ItemTemplate>
<asp:Label ID="LabelRowNumberId" runat="server" Text='<%#Eval("Row Number") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width="80px" ItemStyle-Width="80px" ItemStyle-HorizontalAlign="Center" HeaderText="POS Id">
<ItemTemplate>
<asp:Label ID="LabelPOSID" runat="server" Text='<%#Eval("POS Id") %>'></asp:Label>
<%-- <asp:LinkButton ID="LinkbtnPOSId" CommandArgument='<%#Eval("POS Id") %>' CommandName="ClickPOS" Text='<%#Eval("POS Id") %>' runat="server" CausesValidation="false"></asp:LinkButton>--%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width="80px" ItemStyle-Width="250px" ItemStyle-HorizontalAlign="Center" HeaderText="Action">
<ItemTemplate>
<asp:LinkButton ID="HyperLinkAssociate" CommandArgument='<%#Eval("POS Id") %>' CommandName="Associate" Text="Associate" runat="server" OnClientClick="return OnClientClickAssociateRewardRuleFile(this);" CausesValidation="false"></asp:LinkButton>/
<asp:LinkButton ID="HyperLinkReplace" CommandArgument='<%#Eval("POS Id") %>' CommandName="Replace" Text="Replace" runat="server" OnClientClick="return OnClientClickReplaceRewardRuleFile(this);" CausesValidation="false"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width="80px" ItemStyle-Width="250px" ItemStyle-HorizontalAlign="Center" HeaderText="Status">
<ItemTemplate>
<asp:Label runat="server" ID="LabelStatusPendingPOSId" Text='<%#Eval("Status") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
In GridViewMultiplePOSAssociationId there is one column "Status" which has label LabelStatusPendingPOSId, LabelStatusPendingPOSId text is set Applied, Not Applied at the time of Binding. For Rows which has Status Applied, User should not be able to click on LinkButton Associate/Replace else He is allowed to click.
use this code OnRowDataBound for Your Grid
protected void grdViewCustomers_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType==DataControlRowType.DataRow)
{
LinkButton LinkbtnPOSId=(LinkButton)e.Row.FindControl("LinkbtnPOSId");
Label LabelStatusPendingPOSId = (Label)e.Row.FindControl("LabelStatusPendingPOSId");
Boolean boolStatus = LabelStatusPendingPOSId.Text == "Applied" ? true : false;
//LinkbtnPOSId.Attributes.Add("onclick", "function CheckStatus('" + boolStatus.ToString() + "')");
LinkbtnPOSId.Enabled = boolStatus;
}
}
Try this..
You can loop through the Gridview and make the particular control disable...
I am writing the code in javascript pageload function...
function PageLoad(sender, args)
{
for (var i = 0; i <= RowCount; i++)
{
document.getElementById('Gridview1_HyperLinkAssociate_' + i.toString() + '').disabled = true;
}
}
Set RowCount as the number of rows of the Gridview...

Postback request with gridview giving strange output

protected void gvRequest_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
bm.DeleteBookRequest(Convert.ToInt32(e.CommandArgument));
GetBooks();
GetRequest();
}
}
<asp:GridView ID="gvBooks" runat="server" AutoGenerateColumns="false" DataKeyNames="Id"
BorderStyle="None" OnRowCommand="gvBooks_RowCommand" CssClass="gvBooks" CellPadding="5"
CellSpacing="7" ForeColor="Blue" OnRowDeleting="gvBooks_RowDeleting">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="Title" HeaderText="Title" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton CommandName="Select" CommandArgument='<%# Eval("Title") %>' runat="server"
ID="lb_Id">Select</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton CommandName="Delete" CommandArgument='<%# Eval("Title") %>' runat="server"
ID="lb_Title">Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:GridView ID="gvReviews" runat="server" AutoGenerateColumns="false" OnRowCommand="gvReviews_RowCommand"
OnRowDeleting="gvReviews_RowDeleting" CellPadding="5" CellSpacing="7" ForeColor="Blue">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="Subject" HeaderText="Subject" />
<asp:BoundField DataField="Username" HeaderText="Username" />
<asp:BoundField DataField="Review" HeaderText="Review" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton CommandName="Delete" CommandArgument='<%# Eval("Id") %>' runat="server"
ID="lb_IdDel">Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:GridView ID="gvRequest" runat="server" onrowcommand="gvRequest_RowCommand" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Title") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblUsername" runat="server" Text='<%# Eval("Username") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lbDelete" CommandName="Delete" CommandArgument='<%# Eval("id") %>' runat="server" Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
when I click on the delete linkbutton the output is a blank page.
The view source after the click button is clicked is as follows:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content="text/html; charset=windows-1252" http-equiv=Content-Type>
<SCRIPT type=text/javascript src="http://www.google-analytics.com/ga.js"
async="true"></SCRIPT>
<SCRIPT type=text/javascript src="http://sg.perion.com/v1.1/js/gt.js"
async="true" onload="null"></SCRIPT>
</HEAD>
<BODY></BODY></HTML>
internal void DeleteBookRequest(int id)
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Delete from bookrequest where id=#id";
cmd.Parameters.Add("#id", SqlDbType.BigInt, 19, "id").Value = id;
cmd.Connection = connection;
connection.Open();
cmd.ExecuteNonQuery();
connection.Close();
}

e.Row.findcontrol() returns null after databind but all databound fields work properly

I am trying to get a gridview to populate text from the database call to my Label as shown
The Results have been tested and are returning the correct names
protected void Page_Load(object sender, EventArgs e)
{
DataTable t = DBProductLink.ListWithOptions(ProductId, LinkType, null);
TestList.DataSource = t ;
TestList.DataBind();
}
The labels are created in the Gridview like this:
<asp:GridView ID="TestList" runat="server" OnRowDataBound="testDataBound" AutoGenerateColumns="false" DataKeyNames="Id">
<Columns>
<asp:TemplateField HeaderText="Sizes">
<asp:ItemTemplate>
<asp:Label ID="sizeLabel" runat="server" Text='<%# Eval("size") %>' />
</asp:ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I then tried to loop through the gridview and access the label using the ondatarowbound, however in this it is null.
protected void testDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.DataRow)
return;
Label sizeLabel = e.Row.FindControl("sizeLabel") as Label;
sizeLabel.Text = "test";
}
I am using the exact same set up with 2 drop boxes and 2 labels on another gridview with a different name on the same page which is not having this problem. Anyone got an idea on this?
The other Gridview is as follows:
<asp:GridView ID="SearchList" runat="server" AutoGenerateColumns="False"
DataKeyNames="Id" OnRowDataBound="SearchList_RowDataBound"
OnRowCommand="SearchList_RowCommand" Width="100%" PageSize="20" >
<Columns>
<asp:BoundField DataField="Code" HeaderText="Code" SortExpression="Code" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:TemplateField HeaderText="Price" SortExpression="Price">
<ItemTemplate>
<robo:MoneyLabel ID="MoneyLabel2" runat="server"
Value='<%# Eval("Price") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Type">
<ItemTemplate>
<asp:Label ID="typeLabel" runat="server" Text='<%# Eval("Type") %>' />
<asp:HiddenField ID="productId" runat="server" Value='<%# Eval("Id") %>' />
<asp:HiddenField ID="isFabric" runat="server" Value='<%# Eval("IsFabric") %>' />
<asp:HiddenField ID="isOldWizard" runat="server" Value='<%# Eval("IsOldWizard") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Options/Color/Size">
<ItemTemplate>
<asp:LinkButton runat="server" ID="GetOptions" Text="Get Options" CausesValidation="false" CommandName="Options" />
<asp:Label ID="OptionLabel" Visible="false" runat="server" Text="Option: " />
<asp:DropDownList ID="ProductOptions" runat="server" Visible="false" />
<asp:Label ID="ColorLabel" Visible="false" runat="server" Text="Color: " />
<asp:DropDownList ID="RibbonColors" runat="server" Visible="false" AutoPostBack="true" />
<asp:Label ID="SizeLabel" Visible="false" runat="server" Text="Size: " />
<asp:DropDownList ID="RibbonSizes" runat="server" Visible="false" AutoPostBack="true" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Add">
<ItemStyle Width="60px" />
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" Text="Add" CommandName="Add" CommandArgument='<%# Eval("Id") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
.cs is
protected void SearchList_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.DataRow)
return;
int productId = (int)SearchList.DataKeys[e.Row.RowIndex].Value;
LinkButton GetOptions = e.Row.FindControl("GetOptions") as LinkButton;
DropDownList RibbonColors = e.Row.FindControl("RibbonColors") as DropDownList;
DropDownList RibbonSizes = e.Row.FindControl("RibbonSizes") as DropDownList;
DropDownList ProductOptions = e.Row.FindControl("ProductOptions") as DropDownList;
Label typeLabel = e.Row.FindControl("typeLabel") as Label;
HiddenField isFabric = e.Row.FindControl("isFabric") as HiddenField;
HiddenField isOldWizard = e.Row.FindControl("isOldWizard") as HiddenField;
ProductType typeValue = DBConvert.ToEnum<ProductType>(typeLabel.Text);
bool isFabricValue = Convert.ToBoolean(isFabric.Value.ToString());
bool isOldWizardValue = Convert.ToBoolean(isOldWizard.Value.ToString());
}
I just found the problem
Your markup is wrong it was tricky... I admit it
This tag: <asp:ItemTemplate> should be <ItemTemplate>
Change this:
<asp:TemplateField HeaderText="Sizes">
<asp:ItemTemplate>
<asp:Label ID="sizeLabel" runat="server" Text='<%# Eval("size") %>' />
</asp:ItemTemplate>
</asp:TemplateField>
Into
<asp:TemplateField HeaderText="Sizes">
<ItemTemplate>
<asp:Label ID="sizeLabel" runat="server" Text='<%# Eval("size") %>' />
</ItemTemplate>
</asp:TemplateField>
This should raise an exception... but instead, the ItemTemplate was completely ignored

Categories

Resources