DevExpress开发中保存按钮条件化启用/禁用失效问题求助
Hey there! Let's tackle why your "Enregistrer" button stays enabled no matter the condition. From the code snippet you shared, it looks like you're only handling the Condition == A case, but not explicitly setting the button to disabled when Condition matches B. Here's how to fix this:
1. Simplest Approach: Set ClientEnabled in Code-Behind
The cleanest way to handle this is to set the button's ClientEnabled property directly in your server-side code (like Page_Load or a data binding event). This avoids messy front-end conditionals and ensures all cases are covered:
protected void Page_Load(object sender, EventArgs e) { // Assuming "Condition" is a variable accessible in your code-behind BtSavelisteningScale.ClientEnabled = (Condition == A); // This will enable the button when Condition == A, disable it otherwise (Condition == B) }
2. Fix the Front-End Conditional Syntax
If you prefer to handle it directly in the ASPX markup, make sure you cover both conditions explicitly. Your current code only sets ClientEnabled="true" when Condition is A—when it's B, the button uses its default ClientEnabled="true" value. Here's how to fix that:
Option A: Use a Binding Expression
<td> <dx:ASPxButton ID="BtSavelisteningScale" runat="server" HorizontalAlign="NotSet" ImagePosition="Left" Text="Enregistrer" VerticalAlign="NotSet" ClientEnabled='<%# Condition == A %>' ClientInstanceName="yourClientInstanceName" /> </td>
Then, in your code-behind, trigger the binding (usually in Page_Load):
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BtSavelisteningScale.DataBind(); } // If the condition can change on postback, call DataBind() there too }
Option B: Use an Inline Expression
You can also use a ternary operator directly in the attribute:
<td> <dx:ASPxButton ID="BtSavelisteningScale" runat="server" HorizontalAlign="NotSet" ImagePosition="Left" Text="Enregistrer" VerticalAlign="NotSet" ClientEnabled="<%= Condition == A ? true : false %>" ClientInstanceName="yourClientInstanceName" /> </td>
Quick Troubleshooting Checks
- Double-check that your
Conditionvariable is correctly evaluating to A or B. Sometimes typos (like mismatched case, incorrect enum values, or string comparison issues) can break the logic. - If the condition changes after a postback, make sure you re-set the
ClientEnabledproperty in the appropriate postback event (e.g.,Button_Click,GridView_RowDataBound).
内容的提问来源于stack exchange,提问作者user8805723

