Checkboxfield value controls button visibility - c#

I have a gridview which displays the contents of a database table in rows. There is a CheckboxField there and a Select button. I want to set button visibility to false when checkboxfield is checked.
this is my aspx page:
<asp:DetailsView ID="DetailsViewERgo" runat="server" Height="50px"
Width="100%" AutoGenerateRows="False" CellPadding="4"
DataSourceID="LinqDataSourceErgo" ForeColor="#333333" GridLines="None"
HeaderText="Σύντομη Περιγραφή Επιλεγμένου Έργου">
<Columns>
<asp:CheckBoxField DataField="Diekperewsi" HeaderText="Answered"
SortExpression="Diekperewsi" Visible="True"
ItemStyle-HorizontalAlign="Center">
<ItemStyle HorizontalAlign="Center" />
</asp:CheckBoxField>
<asp:TemplateField HeaderText="Insert Answer" ShowHeader="False">
<ItemTemplate>
<center>
<asp:Button ID="Button1" runat="server" CausesValidation="False"
CommandName="Select" Text="Επιλογή" Visible="true" >
</asp:Button>
</center>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I have tried this but only works with checkboxes
protected void GridViewAitima_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox cb = (CheckBox)e.Row.FindControl("Diekperewsi");
Button b = (Button)e.Row.FindControl("Button1");
if (!cb.Checked)
{
b.Visible = false;
}
else
{
b.Visible = true;
}
}
}

Your code will run on the server side but it looks as if the the AutoPostBack property for your checkbox is not set to true -
AutoPostBack="True"
so when the checkbox is checked the code will not run immediately, it will only run after another event has caused your page to postback.

CheckBoxField has no id thus you can't find it by id, moreover it has no value propertie.
I suggest you use template field just like you used for the button, but instead put a checkbox in it.
so instead of :
<asp:CheckBoxField DataField="Diekperewsi" HeaderText="Answered"
SortExpression="Diekperewsi" Visible="True"
ItemStyle-HorizontalAlign="Center">
<ItemStyle HorizontalAlign="Center" />
</asp:CheckBoxField>
put :
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="Diekperewsi" Enabled="false" Checked='<%#Eval("Diekperewsi")%>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
and you are good

Related

Gridview value to a parameter

I have a Home.aspx and Home.aspx.cs
I have a gridview in my Home.aspx >>>
<asp:GridView ID="DataGridView" runat="server" AutoGenerateColumns="False" ShowFooter="True"
CellPadding="4" ForeColor="#333333" GridLines="None" Height="281px" style="margin-top: 0px" Width="100%"
OnRowCancelingEdit="DataGridView_RowCancelingEdit"
OnRowEditing="DataGridView_RowEditing" OnRowUpdating="DataGridView_RowUpdating" HorizontalAlign="Center"
onrowdatabound="DataGridView_RowDataBound">
<AlternatingRowStyle BackColor="Lavender" ForeColor="#284775" />
<Columns>
<asp:TemplateField>
<HeaderTemplate>Data 1</HeaderTemplate>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate><asp:Label ID="description" runat="server" Text='<%# Bind("description")%>'></asp:Label></ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="Editdescription" runat="server">
<asp:ListItem>--Select--</asp:ListItem>
<asp:ListItem>SINGLE</asp:ListItem>
<asp:ListItem>DOUBLE</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<%-- <FooterTemplate>
</FooterTemplate>--%>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>Data 2</HeaderTemplate>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate><asp:Label ID="pkgcode" runat="server" Text='<%# Bind("pkgcode") %>'></asp:Label></ItemTemplate>
<EditItemTemplate><asp:TextBox ID="Editpkgcode" runat="server" Text='<%# Bind("pkgcode") %>'></asp:TextBox></EditItemTemplate>
<%--<FooterTemplate><asp:TextBox ID="pkgcode" runat="server"></asp:TextBox></FooterTemplate>--%>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>Data 3</HeaderTemplate>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate><asp:Label ID="oprcode" runat="server" Text='<%# Bind("oprcode") %>'></asp:Label></ItemTemplate>
<EditItemTemplate><asp:TextBox ID="Editoprcode" runat="server" Text='<%# Bind("oprcode") %>' ></asp:TextBox></EditItemTemplate>
<%--<FooterTemplate><asp:TextBox ID="oprcode" runat="server"></asp:TextBox></FooterTemplate>--%>
</asp:TemplateField>
</Columns>
</asp:GridView>
In my Home.aspx.cs, I have this >>
protected void DataGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{ }
This is where I plan to update my data from my gridview, but before updating I want to pass the old data to a parameter so user can compare/see the changes he made. For checking purposes so I can see if it really get the data, I put the following inside DataGridView_RowUpdating (and I don't know if it's wrong)>>
GridViewRow row = DataGridView.Rows[e.RowIndex];
string #editpkgcode = (row.FindControl("pkgcode") as Label).Text;
Literal1.Text = "TEST: " + #editpkgcode;
It gives me the error : NullReferenceException was unhandled by user code
The reason why you get NullReferenceException is because when RowUpdating event fires then the EditItemTemplate exists but not ItemTemplate.
Since you defined pkgcode Label in ItemTemplate, therefore this label control is not existing when RowUpdating event fires. But, because the EditItemTemplate exists when this event fires, so you can access the Editpkgcode textbox defined in EditItemTemplate.
Therefore, you should be using the following code in your RowUpdating event in order access the textbox in EditItemTemplate.
GridViewRow row = DataGridView.Rows[e.RowIndex];
string #editpkgcode = (row.FindControl("Editpkgcode") as TextBox).Text;

Access GridView ItemTemplate control ASP.NET

I am using ASP.NET GridView on ASPX page:
<asp:GridView ID="GrdLimitAndUtilization" runat="server" AutoGenerateColumns="False" GridLines="None"
Width="99%" meta:resourcekey="GrdAccountListResource1" OnRowDataBound="GrdLimitAndUtilization_RowDataBound"
ShowHeader="True" rowstyle-cssclass="rowHover" ClientIDMode="Static" CssClass="gridView">
<Columns>
<asp:TemplateField HeaderText="Excess" meta:resourcekey="Excess">
<HeaderStyle HorizontalAlign="Left" Width="120px" CssClass="gridheader" />
<HeaderTemplate> <asp:Label ID="col5a" Text="Excess" runat="server"></asp:Label></HeaderTemplate>
<ItemTemplate>
<asp:HyperLink ID="lnkExcess" runat="server" value='' Text='<%# Bind("Excess") %>'
meta:resourcekey="HyperLink1Resource1"></asp:HyperLink>
<asp:Label ID="Excess_Currency" runat="server" Text='<%# Bind("Currency") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="120px" HorizontalAlign="Left" CssClass="customerProductItemTemp gridviewLeftMargin" />
</asp:TemplateField>
</Columns>
</asp:GridView>
I know I can access GridView column by following code:
GrdLimitAndUtilization.Rows[0].Cells[0].ForeColor = System.Drawing.Color.Green;
BUT, How can I access value/text of only HyperLink control with ID="lnkExcess"??
Thanks.
You can use RowDataBound event like this:-
protected void GrdLimitAndUtilization_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
HyperLink lnkExcess = (HyperLink)e.Row.FindControl("lnkExcess");
//Access hyperlink's properties here.
}
}

GridView Row checkboxes

I have a gridview in my .NET web app:-
<asp:GridView ID="gvwQueues" runat="server" AutoGenerateColumns="false" OnPageIndexChanging="gvwQueues_PageIndexChanging"
PageSize="5" Width="577px">
<Columns>
<asp:BoundField DataField="Text" HeaderText="Text">
<ItemStyle Width="150px"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="ItemNumber" HeaderText="Item Number">
<ItemStyle Width="150px"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="Directory" HeaderText="Directory">
<ItemStyle Width="150px"></ItemStyle>
</asp:BoundField>
<asp:TemplateField HeaderText="IsActive">
<ItemTemplate>
<asp:CheckBox ID="Yes" runat="server" Text = "Yes" />
<asp:CheckBox ID="No" runat="server" Text = "No"/>
<%-- <asp:CheckBoxList ID="IsActive" runat="server">
<asp:ListItem>Yes.</asp:ListItem>
<asp:ListItem>No.</asp:ListItem>
</asp:CheckBoxList>--%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Now I want to use the checkboxes:- "yes" or "no" to call appropriate functions on the server side that would make each item on the gridview row "Active" or "inactive".
Active means the item shows up for all users, Inactive means the item doesnt show up for any users, except for the admin.
Two modifications might me needed to make your logic work.
a. Try using Radiobutton in the place of using Check Boxes so that the user will be able to select ONLY ONE OPTION either 'Yes' or 'No'. Also note you need group these radio buttons like below to let only one of these radio buttons selectable. So replace your 'IsActive' template field with below:
<asp:TemplateField HeaderText="IsActive">
<ItemTemplate>
<asp:RadioButton ID="yesRadioButton" runat="server" Text="Yes" GroupName="IsActiveGroup" OnCheckedChanged="RadioButtonIsActive_CheckedChanged" />
<asp:RadioButton ID="noRadioButton" runat="server" Text="No" GroupName="IsActiveGroup" OnCheckedChanged="RadioButtonIsActive_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
And assign both of its CheckChanged event to RadioButtonIsActive_CheckedChanged method.
b. Below is the code for the Code behind mehtod RadioButtonIsActive_CheckedChanged
protected void RadioButtonIsActive_CheckedChanged(object sender, EventArgs e)
{
foreach (GridViewRow row in gvwQueues.Rows)
{
RadioButton yesRadioButton = (RadioButton)row.FindControl("yesRadioButton");
if (yesRadioButton.Checked)
{
//Make Items Active
}
else
{
//Make Items Inactive
}
}
}
Let me know in case of any queries.
Do you want the check box to call a function when it changes?
Then all you need to do is add OnCheckedChanged
<asp:CheckBox ID="chkStatus" runat="server" Text = "Yes"
OnCheckedChanged="chkStatus_OnCheckedChanged"/>
Serverside you write the function you want it to do
public void chkStatus_OnCheckedChanged(object sender, EventArgs e)
{
//Make active inactive
}
Out side of this are you asking about roles, or binding to a grid?
you can use DataKeyNames for the grid view.
try this once.
<asp:GridView ID="gvwQueues" runat="server" AutoGenerateColumns="false" OnPageIndexChanging="gvwQueues_PageIndexChanging"
PageSize="5" Width="577px" DataKeyNames="ItemNumber">
<Columns>
<asp:BoundField DataField="Text" HeaderText="Text">
<ItemStyle Width="150px"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="ItemNumber" HeaderText="Item Number">
<ItemStyle Width="150px"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="Directory" HeaderText="Directory">
<ItemStyle Width="150px"></ItemStyle>
</asp:BoundField>
<asp:TemplateField HeaderText="IsActive">
<ItemTemplate>
<asp:CheckBox ID="chkYes" runat="server" Text="Yes" OnCheckedChanged="YesOrNo_OnCheckedChanged"
AutoPostBack="true" />
<asp:CheckBox ID="chkNo" runat="server" Text="No" OnCheckedChanged="YesOrNo_OnCheckedChanged"
AutoPostBack="true" />
<%-- <asp:CheckBoxList ID="IsActive" runat="server">
<asp:ListItem>Yes.</asp:ListItem>
<asp:ListItem>No.</asp:ListItem>
</asp:CheckBoxList>--%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And you can catch the Item Number using ClientID of sender argument
protected void YesOrNo_OnCheckedChanged(object sender, EventArgs e)
{
CheckBox chkSender = (CheckBox)sender;
foreach (GridViewRow gvRow in gvwQueues.Rows)
{
if (gvRow.RowType == DataControlRowType.DataRow)
{
CheckBox chkYes = (CheckBox)gvRow.FindControl("chkYes");
CheckBox chkNo = (CheckBox)gvRow.FindControl("chkNo");
if (chkSender.ClientID == chkYes.ClientID || chkSender.ClientID == chkNo.ClientID)
{
int ItemId = Convert.ToInt32(gvwQueues.DataKeys[gvRow.RowIndex].Value);//here is the item number
if (chkNo.Checked)
{
chkYes.Checked = false;
//code to inactive
}
else if (chkYes.Checked)
{
chkNo.Checked = false;
//code to activate
}
}
}
}
}
One more suggestion is better to use Radiobuttons instead of Checkboxes.
You need to handle many conditions by using Checkboxes in your case.

Execute Javascript function from GridView ItemTemplate button click ASP.NET Web Forms

I have a DataGridView that has two ItemTemplate button columns that are dynamically generated. I have code-behind for the both of the buttons that fire when they are clicked, but I also need a javascript function to run when the btnInfo button is clicked
markup:
<asp:GridView ID="gridResutls" runat="server" OnRowCommand="gridResutls_RowCommand" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="strName" HeaderText="Name"/>
<asp:BoundField DataField="strBrand" HeaderText="Brand"/>
<asp:BoundField DataField="strServing" HeaderText="Serving"/>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Button ID="btnInfo" runat="server" CausesValidation="false" CommandName="MoreInfo"
Text="More Info" CommandArgument='<%# Eval("strID") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Button ID="btnAdd" runat="server" CausesValidation="false" CommandName="AddItem"
Text="Add To Log" CommandArgument='<%# Eval("strID") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>'
I have a javascript function called populateLabel() that I need to fire off when btnInfo is clicked.
Any ideas? (I know similar questions have been asked and I looked throughout the posts and tried a few things but nothing seems to work)
EDIT: Here is what the code-behind for the ItemTemplate buttons looks like
protected void gridResutls_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
{
// Kick out if neither of the two dynamically created buttons have been clicked
if (e.CommandName != "MoreInfo" && e.CommandName != "AddItem")
{
return;
}
// If the "More Info" buton has been clicked
if (e.CommandName == "MoreInfo")
{
// Some code
}
// If the "Add To Log" button has been clicked
if (e.CommandName == "AddItem")
{
// Some code
}
}
You can do something like this.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
Button btnAdd = e.Row.FindControl("btnAdd") as Button;
btnAdd.Attributes.Add("OnClick", "populateLabel();");
}
}
markup:
<script type="text/javascript">
function populateLabel() {
alert('hi');
}
</script>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="DSModels" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Button ID="btnInfo" runat="server" CausesValidation="false" CommandName="MoreInfo"
Text="More Info" CommandArgument='<%# Eval("ID") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Button ID="btnAdd" runat="server" CausesValidation="false" CommandName="AddItem"
Text="Add To Log" CommandArgument='<%# Eval("ID") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Use OnClientClick:
<asp:Button ID="btnInfo" runat="server" OnClientClick="populateLabel()" CausesValidation="false" CommandName="MoreInfo"
Text="More Info" CommandArgument='<%# Eval("strID") %>'/>
Example here.

Hidden fields in a grid won't import to a edit form

I've got a grid that only displays one piece of information such as a title. Other fields are hidden. When you click edit a modal popup displays a form and imports the information from the grid for editing. The information in hidden field are not imported though. I don't want to display all the information in the grid because I have only room for the title.
How can I make this work? Thanks. Risho
<asp:GridView ID="gvForum" runat="server" DataSourceID="odsForumApproval" DataKeyNames="id" Width="200px"
RepeatColumns="1" DataKeyField="id" CssClass="gridview"
AutoGenerateColumns="False" GridLines="None" OnSelectedIndexChanged="_OnCommand">
<AlternatingRowStyle CssClass="altbgcolor" />
<Columns>
<asp:BoundField DataField="title" />
<asp:TemplateField >
<ItemTemplate>
<asp:HiddenField runat="server" ID="hfId" Value='<%# Eval("id") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField >
<ItemTemplate>
<asp:HiddenField runat="server" ID="hfDesc" Value='<%# Eval("description") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField >
<ItemTemplate>
<asp:LinkButton ID="lnkbtn" Text="Approve" runat="server" onclick="lnkbtn_Click"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
protected void lnkbtn_Click(object sender, EventArgs e)
{
LinkButton btndetails = sender as LinkButton;
GridViewRow gvrow = (GridViewRow)btndetails.NamingContainer;
lblID.Value = gvrow.Cells[1].Text;
txtTitle.Text = gvrow.Cells[0].Text;
lblMessage.Text = gvrow.Cells[3].Text;
this.ModalPopupExtender1.Show();
}
The Cell-Text is empty if you are using TemplateFields with nested controls . You need to get the reference to the controls and use their appropriate properties(like TextBox.Text or HiddenField.Value). Therefor you can use FindControl on the GridViewRow:
var hfId = (HiddenField)gvrow.FindControl("hfId");
var hfDesc = (HiddenField)gvrow.FindControl("hfDesc");
txtTitle.Text = gvrow.Cells[0].Text;
lblID.Value = hfId.Value;
txtTitle.Text = hfdesc.Value;

Categories

Resources