I'm having a problem calling a modal in asp
I need to set the postbackurl of linkbutton4 from code behind depending on what is selected in the dropdownlist! I have tried putting the postbackurl directlty on the linkbuttons tag it worked but when i change it from the code behind it doesnt BTW i change it when the link button is clicked.
Code behind for the linkbutton:
protected void LinkButton4_Click(object sender, EventArgs e)
{
var a = (Control)sender;
GridViewRow row = (GridViewRow)a.NamingContainer;
string b = row.Cells[0].Text;
Session["C"] = b;
DropDownList ddl =(DropDownList)row.Cells[7].FindControl("DropDownList1");
Session["D"] = ddl.SelectedItem.Text;
LinkButton lb = (LinkButton)row.Cells[7].FindControl("LinkButton4");
if (Session["D"].ToString() == "Upload")
{
lb.PostBackUrl = "preprod_design.aspx#edit";
// Upload();
}
if (Session["D"].ToString() == "Download")
{
Download();
}
infogridbind();
}
Here is the code for aspx :
<asp:GridView ID="GridView2" runat="server" ondatabound="GridView2_DataBound"
onrowdatabound="GridView2_RowDataBound"
onrowcreated="GridView2_RowCreated"
onselectedindexchanged="GridView2_SelectedIndexChanged"
onrowcommand="GridView2_RowCommand" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="SizeSetID" SortExpression="SizeSetID"/>
<asp:BoundField DataField="Revision No." SortExpression="RevisionNo" HeaderText = "Revision No."/>
<asp:TemplateField HeaderText ="Image">
<ItemTemplate>
<asp:Image ID="Image2" runat="server" onError = "this.style.display = 'none';" ImageUrl='<%#"~/ClientPoImage.ashx?autoId="+Eval("[SizeSetID]")%>' Width="50px" Height="40px"/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Size Name" SortExpression="SizeName" HeaderText = "Size Name"/>
<asp:BoundField DataField="Quantity Requested" SortExpression="QuantityRequested" HeaderText ="Quantity Requested"/>
<asp:BoundField DataField="Quantity Received" SortExpression="QuantityReceived" HeaderText="Quantity Received"/>
<asp:BoundField DataField="Balance" SortExpression="Balance" HeaderText="Balance"/>
<asp:TemplateField HeaderText="Action">
<ItemTemplate >
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true">
<asp:ListItem>Upload</asp:ListItem>
<asp:ListItem>Download</asp:ListItem>
<asp:ListItem>Edit</asp:ListItem>
<asp:ListItem>Delete</asp:ListItem>
<asp:ListItem>Request</asp:ListItem>
<asp:ListItem>Receive</asp:ListItem>
</asp:DropDownList>
<asp:LinkButton ID="LinkButton4" runat="server" onclick="LinkButton4_Click">GO</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
You can change PostBackUrl for LinkButton inside DropDownList.SelectedIndexChanged event like this
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
var ddl = (DropDownList)sender;
var row = (GridViewRow)(ddl.NamingContainer);
var lb = (LinkButton)row.FindControl("LinkButton4");
if (ddl.SelectedValue == "Upload")
{
lb.PostBackUrl = "preprod_design.aspx#edit";
}
if (ddl.SelectedValue == "Download")
{
....
}
}
also you need change markup like this
....
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true"
onselectedindexchanged="DropDownList1_SelectedIndexChanged" >
....
Remove AutoPostBack="True" From DropDownList, and in the header of page <%# Page Title="data"... EnableEventValidation="false" %>
After That You just go to click event of Link button and then
GridViewRow gr = (GridViewRow)(((LinkButton)sender).NamingContainer);
DropDownList ddl = (DropDownList)gr.FindControl("DropDownList1");
If(ddl.SelectedValue =="Upload") // or u can use ddl.SelectedItem.Text
{
//Upload();
}
else if(ddl.SelectedValue == "Download")
{
//Download();
}
Related
I'm trying to populate multiple text boxes with data from a gridview when I click the link button (which is in fact the name of one of the fields in each row) but it isn't going through. I'm new to this - literally my first time. Any help would be most appreciated.
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Select")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow selectedRow = GridView1.Rows[index];
AccountNumber.Text = selectedRow.Cells[1].Text;
Name.Text = selectedRow.Cells[1].Text;
Address1.Text = selectedRow.Cells[1].Text;
Address2.Text = selectedRow.Cells[2].Text;
Address3.Text = selectedRow.Cells[3].Text;
PhoneNumber.Text = selectedRow.Cells[4].Text;
FaxNumber.Text= selectedRow.Cells[5].Text;
CurrencyID.Text = selectedRow.Cells[6].Text;
}
}
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False"
DataKeyNames="Agent_Account_No"
DataSourceID="SqlDataSource1"
AlternatingRowStyle-BackColor ="Lavender"
HeaderStyle-BackColor="#9966FF"
AllowSorting="True" HeaderStyle-BorderColor="Black"
HorizontalAlign="Center"
RowStyle-BorderColor="Black"
EmptyDataText="There are no data records to display."
onrowcommand ="GridView1_RowCommand">
<AlternatingRowStyle BackColor="#CCFFCC" />
<Columns>
<asp:BoundField datafield="Agent_Account_No" HeaderText="Account No"
ItemStyle-HorizontalAlign="Center"
ItemStyle-VerticalAlign="Middle"
ItemStyle-Width="50"
SortExpression="Agent_Account_No"
ReadOnly="true">
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"
Width="50px" />
</asp:BoundField>
<asp:TemplateField HeaderText="Name" SortExpression="Agent_Name">
<ItemTemplate>
<asp:LinkButton ID="AgentName" runat="server"
Text='<%# Eval("Agent_Name") %>'
CommandName="Select"
CommandArgument='<%#Bind("Agent_Name") %>'>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
I got it to work this way - using help from this site:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = GridView1.SelectedRow;
AccountNumber.Text = GridView1.DataKeys[row.RowIndex]["Agent_Account_No"].ToString();
......
}
I don't know if this declaration is right, but it works. However now that it works I see a problem that I didn't see before - see my profile and thanks a lot!
I would highly recommend using TemplateFields for all of your columns, like this:
Markup:
<Columns>
<asp:TemplateField HeaderText="Account No" SortExpression="Agent_Account_No">
<ItemTemplate>
<asp:Label ID="LabelAccountNumber" runat="server"
Text='<%# Eval("Agent_Account_No") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
...
<asp:TemplateField HeaderText="Name" SortExpression="Agent_Name">
<ItemTemplate>
<asp:LinkButton ID="AgentName" runat="server"
Text='<%# Eval("Agent_Name") %>'
CommandName="Select"
CommandArgument='<%#Bind("Agent_Name") %>'>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
Now in the RowCommand method, you can use the FindControl() method of the grid view row to get to the text you are interested in, like this:
Code-behind:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Select")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow selectedRow = GridView1.Rows[index];
// Find the account number label to get the text value for the text box
Label theAccountNumberLabel = selectedRow.FindControl("LabelAccountNumber") as Label;
// Make sure we found the label before we try to use it
if(theAccountNumberLabel != null)
{
AccountNumber.Text = theAccountNumberLabel.Text;
}
// Follow the same pattern for the other labels to get other text values
}
}
In my GridView, I have the following columns:
<Columns>
<asp:BoundField DataField="report_type" HeaderText="Report Type"
SortExpression="report_type" />
<asp:BoundField DataField="progress" HeaderText="Progress"
SortExpression="progress" />
<asp:TemplateField HeaderText="..">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" DataValueField="progress">
<asp:ListItem Value="0">Incomplete</asp:ListItem>
<asp:ListItem Value="1">Complete</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
The progress column is just there for demo purposes which will eventually be removed. How do I get the value of the progress to select the correct itemlist in the dropdown?
So if the value of progress is 1, the dropdown should have Complete selected. If the value of the progress is 0, the dropdown should have Incomplete selected.
Add a OnRowDataBound attribute to the gridview in the .aspx page:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="id" OnRowDataBound="GridViewRowEventHandler">
Replace
<asp:BoundField DataField="Progress" HeaderText="Progress"
SortExpression="progress" />
with
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="progress_Flags" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Progress").ToString()%>'/>
</ItemTemplate>
</asp:TemplateField>
In the code behind:
protected void GridViewRowEventHandler(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label flag = (Label)e.Row.FindControl("progress_Flags");
DropDownList myDropDown = (DropDownList)e.Row.FindControl("DropDownList1");
if (flag.Text == "1")
{
myDropDown.SelectedValue = "1";
}
//add more conditions here..
}
}
In RowDataBound event you can use e.Row.FindControl
protected void GridView_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
GridViewRow row = e.Row;
DataRowView dr = row.DataItem as DataRowView;
// now find the control in the row by control ID
DropDownList myDropDown = row.FindControl("DropDownList1") as DropDownList;
}
I am using Nested GridViews where each row in the gridview has child gridView. I am using RowDataBound Event of Parent GridView, to Binding Child GridView. My Problem is that, how to get Child GridView's Button findcontrol value in Parent gridViews RowDataBound Event.
This is my Aspx page
<asp:GridView ID="grdSubClaimOuter" SkinID="GridView" runat="server" Width="100%"
AutoGenerateColumns="false" OnRowDataBound="grdSubClaimOuter_RowDataBound" OnRowCommand="grdSubClaimOuter_RowCommand"
ShowFooter="false" AllowPaging="true" OnPageIndexChanging="grdSubClaimOuter_PageIndexChanging">
<%--<AlternatingRowStyle BackColor="ButtonFace" />--%>
<Columns>
<asp:TemplateField ItemStyle-Width="5%">
<ItemTemplate>
<asp:HiddenField ID="hdnClaimNo" runat="server" Value='<%# Eval("ClaimNo") %>' />
<asp:Image runat="server" ID="img1" ImageUrl="../images/Collapse_plus.png" />
</ItemTemplate>
<ItemStyle Width="5%"></ItemStyle>
</asp:TemplateField>
<asp:GridView ID="grdSubClaim" runat="server" SkinID="GridView" CellPadding="4" Width="100%"
AutoGenerateColumns="false" ShowFooter="false" OnRowEditing="grdSubClaim_RowEditing"
OnRowCommand="grdSubClaim_RowCommand" OnRowDeleting="grdSubClaim_RowDeleted"
AllowPaging="false" >
<%--SkinID="GridView"--%>
<Columns>
<asp:TemplateField FooterStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left">
<HeaderTemplate>
Sub Claim No
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblSubClaimNoValue" Width="" runat="server" Text='<%#Eval("SubClaimNo")%>'></asp:Label>
</ItemTemplate>
<FooterStyle HorizontalAlign="Left" />
</asp:TemplateField>
<asp:Button ID="btnSubrogation" CssClass="groovybutton" runat="server" CommandName="Subrogation"
Text="Subrogation" CommandArgument='<%# Eval("ClaimNo") + "~" + Eval("SubClaimNo")%>' />
<asp:Button ID="btnSalvage" CssClass="groovybutton" runat="server" CommandName="Salvage"
Text="Salvage" CommandArgument='<%# Eval("ClaimNo") + "~" + Eval("SubClaimNo")%>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<AlternatingRowStyle CssClass="" />
<RowStyle CssClass="ob_gBody" />
<HeaderStyle CssClass="gridHeader" />
</asp:GridView>
<asp:Literal runat="server" ID="Literal2" Text="</td></tr>" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
This is My aspx.cs file
protected void grdSubClaimOuter_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[1].Text.ToString() != " ")
{
Literal ltrChild = (Literal)e.Row.FindControl("ltrChild");
System.Web.UI.WebControls.Image img = (System.Web.UI.WebControls.Image)e.Row.Cells[0].FindControl("img1");
ltrChild.Text = ltrChild.Text.Replace("trChildGrid", "trChildGrid" + e.Row.RowIndex.ToString());
string strChildGrid = "trChildGrid" + e.Row.RowIndex.ToString();
e.Row.Cells[0].Attributes.Add("OnClick", "OpenTable('" + strChildGrid + "','" + img.ClientID + "')");
e.Row.Cells[0].RowSpan = 1;
System.Web.UI.WebControls.GridView gvChild = (System.Web.UI.WebControls.GridView)e.Row.FindControl("grdSubClaim");
PolicyProcessor.DAL.Claim.ClaimSubClaim objDALClaimSubClaim = new PolicyProcessor.DAL.Claim.ClaimSubClaim();
PolicyProcessor.BOL.Claim.ClaimSubClaim objInfoClaimSubClaim = new PolicyProcessor.BOL.Claim.ClaimSubClaim();
HiddenField hdnClaimNo = (HiddenField)e.Row.FindControl("hdnClaimNo");
if (hdnClaimNo.Value != "")
{
objInfoClaimSubClaim.ClaimNo = hdnClaimNo.Value;
}
else
{
objInfoClaimSubClaim.ClaimNo = "0";
}
DataSet dsChild;
dsChild = objDALClaimSubClaim.ResultSet(objInfoClaimSubClaim, "SelectInnerGrid");
if (dsChild.Tables[0].Rows.Count > 0)
{
Button btn = (Button)gvChild.FindControl("btnSalvage");
//btn is null how to get text value in btn
btn.ForeColor = System.Drawing.Color.Red;
gvChild.DataSource = dsChild;
gvChild.DataBind();
}
else
{
Helper.EmptyGrid(gvChild, dsChild.Tables[0]);
}
}
}
}
if anyone knows it,please help me solve this problem.thanks in advance.
First get a reference to the child GridView, then use FindControl to get the Button inside it:
foreach (GridViewRow row in grdSubClaimOuter.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
GridView gvChild = (GridView) row.FindControl("grdSubClaim");
// Then do the same method for Button control column
if (gvChild != null)
{
foreach (GridViewRow row in gvChild .Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
Button btn = (Button ) row.FindControl("buttonID");
if (btn != null )
{
// do your work
}
}
}
}
}
}
If you want value in parent grid's data bound event then use either way
1)You can also use your dsChild(Your dataset) and get field value which you are binding for button.
2) Get value of button from gvChild after binding child grid.
No need to loop into parent and child grid.
i am using telerik grid in c#.
this is my code for grid columns.
<telerik:GridTemplateColumn HeaderText="Active" UniqueName="Active">
<ItemTemplate>
<asp:CheckBox ID="chbActive" runat="server"></asp:CheckBox>
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn HeaderText="Order" UniqueName="ORDER_FLAG">
<ItemTemplate>
<asp:CheckBox ID="chbORDER_FLAG" runat="server"></asp:CheckBox>
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn HeaderText="Sell" UniqueName="SELL_FLAG">
<ItemTemplate>
<asp:CheckBox ID="chbSELL_FLAG" runat="server"></asp:CheckBox>
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn HeaderText="Auto GRN" UniqueName="Auto_GRN">
<ItemTemplate>
<asp:CheckBox ID="ChkAutoGrn" runat="server"></asp:CheckBox>
</ItemTemplate>
</telerik:GridTemplateColumn>
now in this case whenever i checked 'Auto GRN' checkbox ,the 'Order' checkbox should unchecked automatically in grid and same condition for reverse senario..
how i will achieve this..
Please check below demo.
.aspx
<telerik:RadGrid ID="RadGrid3" runat="server" AutoGenerateColumns="false" OnNeedDataSource="RadGrid3_NeedDataSource"
OnItemDataBound="RadGrid3_ItemDataBound">
<MasterTableView>
<Columns>
<telerik:GridTemplateColumn>
<ItemTemplate>
<asp:CheckBox ID="Chk1" runat="server" />
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn>
<ItemTemplate>
<asp:CheckBox ID="Chk2" runat="server" />
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridBoundColumn HeaderText="ID" DataField="ID" UniqueName="ID">
</telerik:GridBoundColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
.aspx.cs
protected void RadGrid3_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
dynamic data = new[] {
new { ID = 1, Name ="Name1"},
new { ID = 2, Name = "Name2"},
new { ID = 3, Name = "Name3"}
};
RadGrid3.DataSource = data;
}
protected void RadGrid3_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
GridDataItem item = e.Item as GridDataItem;
CheckBox Chk1 = item.FindControl("Chk1") as CheckBox;
CheckBox Chk2 = item.FindControl("Chk2") as CheckBox;
// default first check box will be checked
Chk1.Checked = true;
Chk1.Attributes.Add("onclick", "checkUncheckManage(this,'" + Chk2.ClientID+ "')");
Chk2.Attributes.Add("onclick", "checkUncheckManage(this,'" + Chk1.ClientID + "')");
}
}
JS
function checkUncheckManage(chkA, chkB) {
var _chkB = document.getElementById(chkB);
_chkB.checked = !chkA.checked;
}
When I perform a radiobuttonclick, I want to set a dropdownlist to become visible. The radiobutton and dropdownlist are within the same datagrid. I am not sure how to do this.
<asp:UpdatePanel ID="updatepanel" UpdateMode="conditional" runat="server">
<ContentTemplate>
<asp:DataGrid ID="DataGrid" AutoGenerateColumns = "false" CssClass="objectSubTitle" ItemStyle-Wrap="true" runat="server" OnItemCommand="handler" ><Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:RadioButton ID ="RadioButton1" Text="Yes" GroupName="creatingNewCard" OnCheckedChanged="RadioButtonYes" AutoPostBack="True" runat="server" />
<asp:DropDownList ID="DropDownList1" Visible="false" runat="server"/>
</ItemTemplate>
</asp:TemplateColumn>
Assuming that they are in an ItemTemplate of a TemplateField and you want to switch vivibility on serverside:
protected void RbCheckedChanged(Object sender, EventArgs e)
{
var radioButton1 = (RadioButton)sender;
var row = (GridViewRow)radioButton1.NamingContainer;
var dropDownList1 = (DropDownList)row.FindControl("DropDownList1");
dropDownList1.Visible = radioButton1.Checked;
}
Sample-GridView:
<asp:GridView ID="GridView1" AutoGenerateColumns="false" OnRowDataBound="Grid_RowDataBound"
runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:RadioButton ID="RadioButton1" runat="server" OnCheckedChanged="RbCheckedChanged" AutoPostBack="true"></asp:RadioButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Edit: as you've edited your question to show that you really use a DataGrid instead of a GridView, the code is similar:
protected void RbCheckedChanged(Object sender, EventArgs e)
{
var radioButton1 = (RadioButton)sender;
var item = (DataGridItem)radioButton1.NamingContainer;
var dropDownList1 = (DropDownList)item.FindControl("DropDownList1");
dropDownList1.Visible = radioButton1.Checked;
}
You can call the Visible property of the DropdwonList in the radio buttons checked changed event like this
protected void RadioButton1_CheckedChanged(Object sender, EventArgs e)
{
var radioButton1= (RadioButton)sender;
var item = (DataGridItem)radioButton1.NamingContainer;
var dropDownList1 = (DropDownList)item.FindControl("DropDownList1");
dropDownList1.Visible = radioButton1.Checked ? true : false;
}